Select a TreeView node from code

G

Guest

Please help!!! I have a TreeView something like this:

Numbers
---Range 1
-------1
-------2
-------3
---Range 2
-------1
-------2
-------3

How can I select the node that reads "1" at range 2 from my VB .NET code?


i find this sample and use it but there is a big problem

it find the 1 but it is not at range 2
it fınd it from range 1
can you help me



Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'look for a node with a text property value of 'Node3'
TreeView1.SelectedNode = GetNode("Node3", TreeView1.Nodes)
'ensure the focus set on the node
TreeView1.Select()
End Sub

Private Function GetNode(ByVal text As String, ByVal parentCollection As TreeNodeCollection) As TreeNode

Dim ret As TreeNode
Dim child As TreeNode

For Each child In parentCollection 'step through the parentcollection
If child.Text = text Then
ret = child
ElseIf child.GetNodeCount(False) > 0 Then ' if there is child items then call this function recursively
ret = GetNode(text, child.Nodes)
End If

If Not ret Is Nothing Then Exit For 'if something was found, exit out of the for loop

Next

Return ret
end function
 
C

Cor

Hi Bafidi,

Please stay in the original thread, that helps with treeviews you know, now
people will stop answering questions from you because maybe it is already
answered in another node when they recognise you do that.

The answer is in your original node.

Cor
 
N

NM

remove this line form your code :
If Not ret Is Nothing Then Exit For 'if something was found, exit out
of the for loop
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top