Windows Develop Bookmark and Share   
 index > Windows Forms Designer > How To Select A TreeNode With Code
 

How To Select A TreeNode With Code

The Visual Basic Usage ofthe TreeViewSelectedNode is as follows:

Dim instance As TreeView
Dim value As TreeNode

value = instance.SelectedNode
instance.SelectedNode = value

I know how to determine which node is selected, but how can I deterine the value of a treenode
in order to select it? That is, how do I get a value for "instance.SelectedNode = value"?


R Evans
skamper  Monday, March 30, 2009 1:18 AM

Hi revans611,

When you want to make a node being selected. You can set a node to the TreeView's SelectedNode property. To get a node you want to select from a TreeView control. You can visit its nodes property.

Here is the example to select the third child node in a treeview.
Dim value As TreeNode = treeView1.Nodes(2).Nodes(2)
treeView1.SelectedNode = value
treeView1.Focus()

If you want to find a treenode by its text, you can use the following code to select that node.
Private Sub FindNode(ByVal treeView As TreeView, ByVal nodeText As String)
Dim nodeStack As New Stack(Of TreeNode)()
For Each tn As TreeNode In treeView.Nodes
nodeStack.Push(tn)
Next

While nodeStack.Count <> 0
Dim treeNode As TreeNode = nodeStack.Pop()
If treeNode.Text = nodeText Then
treeView.SelectedNode = treeNode
Exit While
End If

For Each tn As TreeNode In treeNode.Nodes
nodeStack.Push(tn)
Next
End While
End Sub

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Tuesday, March 31, 2009 5:17 AM

Hi revans611,

When you want to make a node being selected. You can set a node to the TreeView's SelectedNode property. To get a node you want to select from a TreeView control. You can visit its nodes property.

Here is the example to select the third child node in a treeview.
Dim value As TreeNode = treeView1.Nodes(2).Nodes(2)
treeView1.SelectedNode = value
treeView1.Focus()

If you want to find a treenode by its text, you can use the following code to select that node.
Private Sub FindNode(ByVal treeView As TreeView, ByVal nodeText As String)
Dim nodeStack As New Stack(Of TreeNode)()
For Each tn As TreeNode In treeView.Nodes
nodeStack.Push(tn)
Next

While nodeStack.Count <> 0
Dim treeNode As TreeNode = nodeStack.Pop()
If treeNode.Text = nodeText Then
treeView.SelectedNode = treeNode
Exit While
End If

For Each tn As TreeNode In treeNode.Nodes
nodeStack.Push(tn)
Next
End While
End Sub

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Tuesday, March 31, 2009 5:17 AM

You can use google to search for other answers

Custom Search

More Threads

• Errorprovider and Tooltip in design time?
• Datagrid Combobox Column
• Error with windows forms with C#
• passing reference to a Form
• Show HTML page in PrintPreviewDialog or PrintPreviewControl Control
• Design time events
• accessing items in a listbox by double-clicking them
• Reference Collection TypeConverter?
• Hosting Form Designer + Creating a TabControl with no TabPages
• How to Open "Find And Replace" Dialog Box in VB.Net?