set treeview path

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

is there any way to select a node

i want to selec


1,
1,1,
1,1,2 i want to select this with programaticall

2,
2,1,
2,1,
2,1,
 
Hi Bafidi,

When you send a message it is better to repeat the text from the subject,
often although with me, (and you saw also with OHM) it is not readed.

I hope this answer helps you?

Cor
\\\
Dim mnode As TreeNode = _
TreeView1.Nodes(0).Nodes(0).Nodes(0)
TreeView1.SelectedNode = mnode
TreeView1.Select()
///
 
* =?Utf-8?B?YmFmaWRp?= said:
is there any way to select a node

i want to select

1
1,1
1,1,1
1,1,2 i want to select this with programatically
2
2,1
2,1,1
2,1,2
2,1,3

Does your text show the caption of the node?

A very general approach would be to store all 'TreeNode's in a
'Hashtable' You can use the node's 'FullPath' as key.
 
Hi Herfried,
A very general approach would be to store all 'TreeNode's in a
'Hashtable' You can use the node's 'FullPath' as key.

See my code that is a little bit more simple.

Cor
 
* "Cor said:
See my code that is a little bit more simple.

I saw it, it's another approach which expects that you know the exact
position of the node (which can change).
 
bafidi,
In addition to Cor's & Herfried's examples,

You could "walk" the Node.Nodes collection to find your node of interest.
However depending on the amount of data & how often I wanted to select a
specific node I might go with either Cor's or Herfried's suggestion.

Here's a custom Tree View that adds a SelectedPath property, instead of
putting a TreeView on your Form, put a MyTreeView (I will add a TreeView in
the designer then carefully change System.Windows.Forms.TreeView to
MyTreeView), then in code or the properties window enter the path of the
requested Node.

Private Sub BafidiForm_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
TreeView1.SelectedPath = "1\1,1\1,1,2"
End Sub

Option Strict On
Option Explicit On

Public Class MyTreeView
Inherits TreeView

<System.ComponentModel.DefaultValue("")> _
Public Property SelectedPath() As String
Get
If MyBase.SelectedNode Is Nothing Then
Return String.Empty
Else
Return MyBase.SelectedNode.FullPath
End If
End Get
Set(ByVal value As String)
If value = String.Empty Then
MyBase.SelectedNode = Nothing
Exit Property
End If
Dim paths() As String = Split(value, MyBase.PathSeparator)
Dim nodes As TreeNodeCollection
Dim node As TreeNode
For Each path As String In paths
If nodes Is Nothing Then
nodes = MyBase.Nodes
ElseIf Not node Is Nothing Then
nodes = node.Nodes
End If
For Each child As TreeNode In nodes
If child.Text = path Then
node = child
Exit For
End If
Next
Next
If value = node.FullPath Then
MyBase.SelectedNode = node
Else
Throw New ArgumentOutOfRangeException("SelectedPath", value,
"SelectedPath not found")
End If

End Set
End Property

End Class


Hope this helps
Jay
 
Back
Top