TreeView

When working with a treeview, I understand how to select a node by clicking on it.  However, if you are working with your tree in code, I am confused how to address/select a specific node.  Say I wish to loop thru my tree tree starting at a specified point and collapse all the child nodes of it that have have children.

Example:

Base
    Level 1
        Level 2
            Level 2.1
            Level 2.2
        Level 3
        Level 4 

Can I see a code example of how to select node "Level 2" by name without respect to its position in the tree.  I want to be able to address specific nodes without clicking on them or iterating to them. 

Thanks,
Fred H.




Answer this question

TreeView

  • Mark Wistrom

    Hi Stephen

    To which version of VS you are reffering VS 2003 treeview doesn’t support key collection.

  • FvS

    It's generally not a good idea to refer to a node by its text.  What if you later change the text, or need to localize it into different languages, or it is affected by the numeric formatting options on the user's machine

    You should add your nodes with keys in addition to text.  Then you can access specific nodes by key.

    For example:

            TreeView1.Nodes.Add("key1", "some text")
            TreeView1.Nodes.Add("key2", "some other text")

            TreeView1.Nodes("key1").BackColor = Color.Coral
            TreeView1.Nodes("key2").BackColor = Color.Cornsilk



  • TreeView