Expand a TreeView at a specific child node

HichamDotNet

Member
Joined
Mar 10, 2011
Messages
9
Programming Experience
3-5
Hi,
I have a TreeView with [C:] drive folders hierarchy
How can I make the TreeView expand to DESKTOP folder by code at Form Load
Thank you for your help
 
After long hours of search I found a quite good example
There may be something simpler but this one is not bad
The idea is to browse nodes in a RECURSIVE manner
Here's a very simple example:
VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   Dim Nodes As TreeNodeCollection = TreeView1.Nodes
   Dim Node As TreeNode
   For Each Node In Nodes
      FindNode(Node)
   Next
End Sub
 
Private Sub FindNode(ByVal tNode As TreeNode)
   Dim tn As TreeNode
   For Each tn In tNode.Nodes
      If tn.Text = "Desktop" Then
         TreeView1.SelectedNode = tn
         TreeView1.SelectedNode.Expand()
         Exit For
      End If
      FindNode(tn)
   Next
End Sub
 
Last edited:
TreeNodeCollection.Find method is already recursive and uses the nodes Name as key. So if you know the key exists you can do this:
VB.NET:
Me.FoldersTreeView.SelectedNode = Me.FoldersTreeView.Nodes.Find("desktop", True)(0)
 
Thank you for your answer
I already used the FIND method in several ways but it works only with the first level nodes, (True as second parameter)
if I try with a ChildNode I have the error "Index out of range exception"
I don't know why and what I should do
Can you please enlighten me. Thank you.
 
I don't know what you did so I can't say what you did wrong. I did however post sample code that works if there is a node with Name "desktop" at any level.
 
I used your code sample in a new Form with a TreeView named TreeView1 and a button named button1
So here's what it looks like:
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
    Me.TreeView1.SelectedNode = Me.TreeView1.Nodes.Find("Desktop", True)(0)
    Me.TreeView1.SelectedNode.Expand()
End Sub
This works only with the first level.
From Level two and on, the error I told you occurs at the find method line.
If you see something wrong please tell me.
Thank you.
 
If that code don't work it means you no node with Name property set to "Desktop" anywhere in tree.
 
Thank you very much, so I will keep the first example above which works fine and forget the Find method.
Thank you for your answers.
 
So basically you'd rather write 10 lines of redundant code than adding one word in code to set a Name for your nodes?
 
I have no choice because I can't get the method FIND to work
it gives me the error "Index out of range"
I spent hours trying to figure out what is wrong with it
I found various examples on the net that I tried, same thing, same error
Maybe it's a problem of version ! I have Visual Studio 2010, .netframework 4
As for the recursive procedure above, I don't think it's redundant, it just points to every node and childnode one by one
I used the debbuger to see how it works and noticed nothing inconvenient
I don't want to bother you but I would really appreciate it if you could post a working example, a very short one
A TreeView and Button
 
As I have explained to you five times already you have to set the Name property. You can set the Name property for a TreeNode using the instance property:
VB.NET:
node.Name = "name"
or using the Add method of TreeNodeCollection:
VB.NET:
.Nodes.Add("name", "text")
I recommend you take the tour in help for TreeView class, TreeNode class and TreeNodeCollection class. To use the TreeView control you need to know these classes.
 
At last I understand, I'm a bad student.
So I have to use the NAME property instead of the text displayed in the treeview
Now it's working with only one line instead of ten.
Thank you VERY much John
 
Yes, as I said, Find method uses the Name of the node as key.
 
Back
Top