הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Tuesday, October 7, 2014 7:27 PM
Hi,
struggling with this one... I have I treeview control... I want to pass it a known (treeview node path) and I want to select that node and set the focus there....
having a really difficult time trying to figure this out... any help?
All replies (8)
Wednesday, October 8, 2014 11:04 AM ✅Answered
Hi,
I am not sure why you want to use a full path like "Node1.Node2.Node3" because, as long as your nodes have names and you know the name of the node you want selected you can just use "Node3" in the TreeView.Find method to select the node. However, if you want to be able to set your property using a FullPath to the node or by just using the name of the node you can try it like the example below.
I changed the ChangeSelection sub so that it splits the FullPath on the "." character and then it uses the last Node`s name in the TreeView.Find method to select the node. I also set the TreeView`s PathSeparator character to the "." character so that it is compatible. Meaning you can set the Selected property with the same string that is returned from the Selected property. I also changed the Property to include some error prevention so you don`t get warnings in the designer about the "Object not set to an instance" which i was getting when testing your code.
I also would recommend not using the name of an actual control class as the name of your control. I would change "TreeView" to "TreeView1" or some other unique name.
Here is the MyTreeView UserControl code
Public Class MyTreeView
Private Sub MyTreeView_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TreeView1.HideSelection = False
TreeView1.PathSeparator = "."
End Sub
Public Shadows Property Selected() As String
Get
If TreeView1.SelectedNode IsNot Nothing Then
Return TreeView1.SelectedNode.FullPath
Else
Return String.Empty
End If
End Get
Set(ByVal value As String)
If value.Trim <> String.Empty Then
ChangeSelection(value)
End If
End Set
End Property
Private Sub ChangeSelection(ByVal nodepath As String)
Dim np() As String = nodepath.Split(CChar("."))
Dim tn() As TreeNode = TreeView1.Nodes.Find(np(np.Length - 1), True)
If tn.Length > 0 Then
TreeView1.SelectedNode = tn(0)
End If
End Sub
End Class
Here is the code i used in the Form to Set and Get the Selected property.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MyTreeView1.Selected = "Node1.Node2.Node3"
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
MessageBox.Show(MyTreeView1.Selected)
End Sub
End Class
If you say it can`t be done then i`ll try it
Tuesday, October 7, 2014 7:33 PM
Hi,
struggling with this one... I have I treeview control... I want to pass it a known (treeview node path) and I want to select that node and set the focus there....
having a really difficult time trying to figure this out... any help?
Use the TreeView's .AfterSelect event.
In that event handler sub, put the following:
Stop
When it stops there, inspect "e" and check out the various event arguments. You can use the .FullPath of the TreeView node to uniquely distinguish which node was selected.
Still lost in code, just at a little higher level.
:-)
Tuesday, October 7, 2014 7:42 PM | 1 vote
Hi,
struggling with this one... I have I treeview control... I want to pass it a known (treeview node path) and I want to select that node and set the focus there....
having a really difficult time trying to figure this out... any help?
I read that wrong.
You want to find the node that you pass it, right?
Use this method but note that you have to give each node a name - which isn't the default unless they've changed it.
Still lost in code, just at a little higher level.
:-)
Tuesday, October 7, 2014 7:42 PM | 1 vote
Hi,
struggling with this one... I have I treeview control... I want to pass it a known (treeview node path) and I want to select that node and set the focus there....
having a really difficult time trying to figure this out... any help?
I don't know how but I suspect something in the TreeNode Class would be used for that. I don't know how focus would work though.
Also see TreeView Class and I was unaware until this that there was a separate TreeNode class.
tutorialspoint - VB.Net - TreeView Control
La vida loca
Tuesday, October 7, 2014 8:12 PM | 1 vote
Hi,
As long as you have the nodes named and know the name of the node then you can do this.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TreeView1.HideSelection = False
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim tn() As TreeNode = TreeView1.Nodes.Find("Node5", True)
If tn.Length > 0 Then
TreeView1.SelectedNode = tn(0)
End If
End Sub
End Class
You can`t see it good but, Node5 is selected.
If you say it can`t be done then i`ll try it
Wednesday, October 8, 2014 12:33 AM
not sure it this would help... but I have created a custom user control with the treeview.... so in my control I have this...
Public Shadows Property Selected As String
Get
Return TreeView.SelectedNode.ToString
End Get
Set(value As String)
ChangeSelection(value)
End Set
End Property
Private Sub ChangeSelection(ByVal value As String)
Dim tn() As TreeNode = Me.TreeView.Nodes.Find("Node1.Node2.Node3", True)
If tn.Length > 0 Then
TreeView.SelectedNode = tn(0)
End If
End Sub
Then on the test from I have , TreeView1 is define as my treeview control..
Me.TreeView1.Selected = Me.txtPathText.Text
Wednesday, October 8, 2014 10:03 AM
not sure it this would help... but I have created a custom user control with the treeview.... so in my control I have this...
Public Shadows Property Selected As String Get Return TreeView.SelectedNode.ToString End Get Set(value As String) ChangeSelection(value) End Set End Property Private Sub ChangeSelection(ByVal value As String) Dim tn() As TreeNode = Me.TreeView.Nodes.Find("Node1.Node2.Node3", True) If tn.Length > 0 Then TreeView.SelectedNode = tn(0) End If End Sub
Then on the test from I have , TreeView1 is define as my treeview control..
Me.TreeView1.Selected = Me.txtPathText.Text
You added this event handler ChangeSelection to that treeview?
Why not just place them to the one you are calling Me.TreeView1.Selected = Me.txtPathText.Text and replace the "Node1.Node2.Node3" to the textbox's text ?
remember make the reply as answer and vote the reply as helpful if it helps.
Wednesday, October 8, 2014 3:07 PM
Thank you very much.... exactly what I was trying to achive.