Home » C# ProgrammingRSS

Focus selected node in treeview by c#

Hi ,

How can Andall choice in one of the branches of the treeView ?,

In other words :

 how can focus  selected node in one of the nodes of treeview by c#?

thanks.

Ashraf

 

 

 

 

5 Answers Found

 

Answer 1

Hi,

Use TreeView.SelectedNode property. e.g. yourTreeView.SelectedNode = yourTreeView.Nodes[0] will select the first tree node.

Regards,

Yasser.

 

Answer 2

Hi Yasser,

 The problem is simply that I have two controls on my Form1  : ( textBox1 and Treeview1)  ...

Treeview1 nodes is : Parent1 in level 1  .... and Child1, Child2 , Child3  and Child4 in level2

Now:  

I want  if I write Child3 in textBox1.Text ..... ( if textBox1.Text = Child3 ....Or any node  )

Corsur select Child3 in Treeview1 (I meant Child3 is shaded ....(selected))

I hope u got my question ... and thanks very much

Ashraf

 

Answer 3

hi ashraf

try this it will help you-

TreeNode[] tt = this.treeView1.Nodes.Find("Child3", true);
this.treeView1.SelectedNode = tt[0];

 

regards

jayant

 

Answer 4

Thank you. I got now.

I provided an example which does your desire as below:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
  public class Form1 : Form
  {
    TreeView tv = new TreeView() { Dock = DockStyle.Top };
    TextBox tb = new TextBox() { Dock = DockStyle.Bottom };
    public Form1()
    {
      this.Controls.AddRange(new Control[] { tb, tv });
      tv.Size = new Size(tv.Size.Width, this.ClientSize.Height - tb.Size.Height);
      for (int parents = 0; parents < 10; parents++)
      {
        string pname = string.Format("P{0}", parents);
        TreeNode parent = new TreeNode(pname);
        parent.Name = pname;
        for (int childs = 0; childs < 10; childs++)
        {
          string cname = string.Format("{0}C{1}", pname, childs);
          TreeNode child = new TreeNode(cname);
          child.Name = cname;
          parent.Nodes.Add(child);
        }
        tv.Nodes.Add(parent);
      }
      tv.ExpandAll();

      tb.TextChanged += new EventHandler(tb_TextChanged);
    }

    void tb_TextChanged(object sender, EventArgs e)
    {
      TreeNode[] tns = tv.Nodes.Find(tb.Text, true);
      if (tns.Length > 0)
      {
        tv.SelectedNode = tns[0];
        tv.SelectedNode.EnsureVisible();  //scroll if necessary
        tv.Focus();
      }
    }
  }
}

Sincerely,

Yasser.

 

Answer 5

Hey,

I want to do exactly the opposite—I have a treeview  representing short phrases in a Word document.  When I click an element I want a textbox to show the origin sentence of the short phrase, and the short phrase selected  in the textbox.  The textbox is called detailBox and I am using detailBox.Focus().  Just to be clear, this method has worked fine when the event I am picking up is an ordinary button; the treenode seems to be retaining focus, though.  My code is a bit like:

 

privatevoid treeview_AfterSelect(object sender, TreeViewEventArgs e)
{

 //...if(e.Node.Tag != null)
 {
  //...//xrTreeNodeContainer is an object containing information//I might expect to retrieve from a TreeNode
  currentError=((xrTreeNodeContainer)e.Node.Tag).getError();
  currentSentence=((xrTreeNodeContainer)e.Node.Tag).getRange();

  detailBox.Text=currentSentence.getString();
  if(currentError!=null)
  {
   detailBox.Select(currentError.start() - currentSentence.start(), currentError.length());
   detailBox.Focus(); //THIS RETURNS TRUE
  }
  //...
 }
 //...
}

 

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter