How to select a node of treeview programmatically in c#?
Asked Answered
R

7

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"); 
}
Rennes answered 17/1, 2011 at 12:53 Comment(3)
You'll find an example here.Bowes
It didnt work in my case.....Rennes
AfterSelect fires whether you select the node in code or the user does it. Post better repro code.Huffish
M
57

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());
    }


}
Mohock answered 17/1, 2011 at 13:10 Comment(5)
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..Rennes
Iam adding nodes and selecting them in the constructor so failed to invoke afterselect event. Got my mistake.. Thanks a lotRennes
Yes, constructor is not quite the same as Form Load.Mohock
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.Lapith
Thank you - following the SelectedNode setter with a call to treeView1.Focus() makes this work greatChud
S
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.

Subirrigate answered 19/12, 2018 at 19:35 Comment(1)
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.Noddle
S
0

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

Scutage answered 17/1, 2011 at 12:56 Comment(1)
Maybe you meant "hook to AfterSelect event..."Contemn
R
0

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

Romp answered 12/3, 2013 at 8:42 Comment(0)
R
0

I had the same problem right now and managed it this way.

TreeNodeCollection nodes = treeView1.Nodes;
foreach (TreeNode node in nodes)
{
    if(node.Text == "My Nodename")
    {
        treeView1.SelectedNode = node;
        treeView1.Select();
        break;
    }
}

Hope that helps someone.

Ringmaster answered 9/2 at 13:41 Comment(0)
B
-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
Bate answered 29/4, 2019 at 21:9 Comment(0)
D
-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();
}
Daphne answered 9/1, 2014 at 13:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.