34

Used treeview.SelectedNode to select a child node. How to invoke treeview.AfterSelect event when a node is selected programmatically?

this.treeView1.SelectedNode = this.treeView1.Nodes[0].Nodes[0].Nodes[0].Nodes[0]; 
if (this.treeView1.Nodes[0].Nodes[0].Nodes[0].Nodes[0].IsSelected) 
{
 MessageBox.Show("Node is selected"); 
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
user186246
  • 1,857
  • 9
  • 29
  • 45

6 Answers6

53

Apologies for my previously mixed up answer.

Here is how to do:

myTreeView.SelectedNode = myTreeNode;

(Update)

I have tested the code below and it works:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        treeView1.Nodes.Add("1", "1");
        treeView1.Nodes.Add("2", "2");
        treeView1.Nodes[0].Nodes.Add("1-1", "1-1");
        TreeNode treeNode = treeView1.Nodes[0].Nodes.Add("1-2", "1-3");
        treeView1.SelectedNode = treeNode;
        MessageBox.Show(treeNode.IsSelected.ToString());
    }


}
Aliostad
  • 80,612
  • 21
  • 160
  • 208
  • I have tried selecting a node aas you said but it is returning as false when I called isSelected() of that node after it was selected.. – user186246 Jan 17 '11 at 13:12
  • Iam adding nodes and selecting them in the constructor so failed to invoke afterselect event. Got my mistake.. Thanks a lot – user186246 Jan 17 '11 at 13:37
  • Yes, constructor is not quite the same as Form Load. – Aliostad Jan 17 '11 at 13:39
  • 2
    Something I learned was that when you select the node programatically and it doesn't appear selected (blue) it's possible due to the treeview not being the active control. – SingleStepper May 08 '20 at 14:54
  • 1
    Thank you - following the `SelectedNode` setter with a call to `treeView1.Focus()` makes this work great – chossenger Aug 22 '22 at 11:33
5
treeViewMain.SelectedNode = treeViewMain.Nodes.Find(searchNode, true)[0];

where searchNode is the name of the node. I'm personally using a combo "Node + Panel" where Node name is Node + and the same tag is also set on panel of choice. With this command + scan of panels by tag i'm usually able to work a treeview+panel full menu set.

  • Welcome to stackoverflow. Because this question already have several answers, consider adding details to your answer as to why your answer and not one of the existing ones. – Simon.S.A. Dec 19 '18 at 20:09
0

yourNode.Toggle(); //use that function on your node, it toggles it

ollo
  • 24,797
  • 14
  • 106
  • 155
Siim Nelis
  • 882
  • 7
  • 10
0

Call the TreeView.OnAfterSelect() protected method after you programatically select the node.

Liviu Mandras
  • 6,540
  • 2
  • 41
  • 65
-1
TreeViewItem tempItem = new TreeViewItem();
TreeViewItem tempItem1 = new TreeViewItem(); 
tempItem =  (TreeViewItem) treeView1.Items.GetItemAt(0);    // Selecting the first of the top level nodes
tempItem1 = (TreeViewItem)tempItem.Items.GetItemAt(0);      // Selecting the first child of the first first level node
SelectedCategoryHeaderString = tempItem.Header.ToString();  // gets the header for the first top level node
SelectedCategoryHeaderString = tempItem1.Header.ToString(); // gets the header for the first child node of the first top level node
tempItem.IsExpanded = true;         //  will expand the first node
vik_78
  • 1,107
  • 2
  • 13
  • 20
-3
private void btn_CollapseAllAndExpandFirstLevelUnderRoot(object sender, EventArgs e)

{
    //this example collapses everything, then expands the first level under the root node.

    tv_myTreeView.CollapseAll();
    TreeNode tn =  tv_myTreeView.Nodes[0];
    tn.Expand();
}
David Gorsline
  • 4,933
  • 12
  • 31
  • 36
rynaskir
  • 9
  • 1