473,473 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Select a treeview node by its path

Hi there.
I know you can view a node's fullpath property, but is it posible to select
a node using its path?
Like, tell the treeview that the node that should be selected is the node
with the path "Users\Administrators\John Doe"?
Thank you in advance!

Andre Nogueira
Nov 21 '05 #1
13 9029
* "André Nogueira" <an**@netcabo.pt.NOSPAM> scripsit:
I know you can view a node's fullpath property, but is it posible to select
a node using its path?
Like, tell the treeview that the node that should be selected is the node
with the path "Users\Administrators\John Doe"?


Set up a hashtable that stores (fullpath, 'TreeNode') pairs. Then you
can lookup the treenodes by their path and set the treeview's
'SelectedNode' property to the specific node.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #2
Thanks!
I've never used hashtables before, but I'll look it up.

Andre Nogueira

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:uh**************@tk2msftngp13.phx.gbl...
* "André Nogueira" <an**@netcabo.pt.NOSPAM> scripsit:
I know you can view a node's fullpath property, but is it posible to
select
a node using its path?
Like, tell the treeview that the node that should be selected is the node
with the path "Users\Administrators\John Doe"?


Set up a hashtable that stores (fullpath, 'TreeNode') pairs. Then you
can lookup the treenodes by their path and set the treeview's
'SelectedNode' property to the specific node.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #3
Worked like a charm!
Thank you once more for all the times you had the time and patience needed
to help me out!
You really deserve the MVP status and its people like you that save the
lifes of people like me =)

André Nogueira

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:uh**************@tk2msftngp13.phx.gbl...
* "André Nogueira" <an**@netcabo.pt.NOSPAM> scripsit:
I know you can view a node's fullpath property, but is it posible to
select
a node using its path?
Like, tell the treeview that the node that should be selected is the node
with the path "Users\Administrators\John Doe"?


Set up a hashtable that stores (fullpath, 'TreeNode') pairs. Then you
can lookup the treenodes by their path and set the treeview's
'SelectedNode' property to the specific node.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #4
I'm having a problem.
This is the code I am using

<...>
Dim a As SizeTreeNode = HashTable.Item(path)
Me.TreeView1.SelectedNode = a
a.Parent.ExpandAll
<...>

Why doesn't this work? What am I doing wrong?
The node is neither selected nor expanded.
If I do

MsgBox(a.Text)
MsgBox(Me.TreeView1.SelectedNode.Text)
I see that "a" is the correct node, but the selected node is the node that
was selected prior to that statement. So what am I missing here?
Thanks again!

André Nogueira

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:uh**************@tk2msftngp13.phx.gbl...
* "André Nogueira" <an**@netcabo.pt.NOSPAM> scripsit:
I know you can view a node's fullpath property, but is it posible to
select
a node using its path?
Like, tell the treeview that the node that should be selected is the node
with the path "Users\Administrators\John Doe"?


Set up a hashtable that stores (fullpath, 'TreeNode') pairs. Then you
can lookup the treenodes by their path and set the treeview's
'SelectedNode' property to the specific node.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B URL:http://dotnet.mvps.org/dotnet/faqs/

Nov 21 '05 #5
* "André Nogueira" <an**@netcabo.pt.NOSPAM> scripsit:
This is the code I am using

<...>
Dim a As SizeTreeNode = HashTable.Item(path)
Me.TreeView1.SelectedNode = a
a.Parent.ExpandAll
<...>

Why doesn't this work? What am I doing wrong?
The node is neither selected nor expanded.
Set the treeview's 'HideSelection' property to 'False' to check if the
node is selected even if the treeview control doesn't have the focus.
If I do

MsgBox(a.Text)
MsgBox(Me.TreeView1.SelectedNode.Text)
I see that "a" is the correct node, but the selected node is the node that
was selected prior to that statement. So what am I missing here?


Are you sure you placed this code after setting the 'SelectedNode'? To
check if the selected node has been set, use 'If
Me.TreeView1.SelectedNode Is a Then...' after setting the selected node.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #6
André,
In addition to the other comments.

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 the other 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.

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
"André Nogueira" <an**@netcabo.pt.NOSPAM> wrote in message
news:ek**************@TK2MSFTNGP15.phx.gbl...
Hi there.
I know you can view a node's fullpath property, but is it posible to select a node using its path?
Like, tell the treeview that the node that should be selected is the node
with the path "Users\Administrators\John Doe"?
Thank you in advance!

Andre Nogueira

Nov 21 '05 #7
This is the code I'm using

Dim a As SizeTreeNode = HT.Item(Lvi.path)
Me.TreeView1.SelectedNode = a
MsgBox(Me.TreeView1.SelectedNode Is a)
MsgBox(a.Text)
MsgBox(Me.TreeView1.SelectedNode.Text)
a.Parent.ExpandAll

The first msgbox returns "false", the other two return what I have told you.
So... What is the problem?
SizeTreeNode is a class that inherits from the treenode but adds a few
properties I need for my program. All nodes on the treeview are
SizeTreeNodes and the values added to the hashtable are also SizeTreeNodes.

André Nogueira
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:e2**************@TK2MSFTNGP15.phx.gbl...
* "André Nogueira" <an**@netcabo.pt.NOSPAM> scripsit:
This is the code I am using

<...>
Dim a As SizeTreeNode = HashTable.Item(path)
Me.TreeView1.SelectedNode = a
a.Parent.ExpandAll
<...>

Why doesn't this work? What am I doing wrong?
The node is neither selected nor expanded.


Set the treeview's 'HideSelection' property to 'False' to check if the
node is selected even if the treeview control doesn't have the focus.
If I do

MsgBox(a.Text)
MsgBox(Me.TreeView1.SelectedNode.Text)
I see that "a" is the correct node, but the selected node is the node
that
was selected prior to that statement. So what am I missing here?


Are you sure you placed this code after setting the 'SelectedNode'? To
check if the selected node has been set, use 'If
Me.TreeView1.SelectedNode Is a Then...' after setting the selected node.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #8
If I do

a = Me.TreeView1.SelectedNode.Parent
Me.TreeView1.SelectedNode = a
MsgBox(Me.TreeView1.SelectedNode Is a)
MsgBox(a.Text)
MsgBox(Me.TreeView1.SelectedNode.Text)

It works.
I did this to see if it was a problem with the selectednode property or
something. This sets the selected node to parent of the currently selected
node and it works.
This isn't what I want to do, this is just an experiment.
So what am I doing wrong with my other code?

André Nogueira
"André Nogueira" <an**@netcabo.pt.NOSPAM> wrote in message
news:uV**************@TK2MSFTNGP15.phx.gbl...
This is the code I'm using

Dim a As SizeTreeNode = HT.Item(Lvi.path)
Me.TreeView1.SelectedNode = a
MsgBox(Me.TreeView1.SelectedNode Is a)
MsgBox(a.Text)
MsgBox(Me.TreeView1.SelectedNode.Text)
a.Parent.ExpandAll

The first msgbox returns "false", the other two return what I have told
you.
So... What is the problem?
SizeTreeNode is a class that inherits from the treenode but adds a few
properties I need for my program. All nodes on the treeview are
SizeTreeNodes and the values added to the hashtable are also
SizeTreeNodes.

André Nogueira
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:e2**************@TK2MSFTNGP15.phx.gbl...
* "André Nogueira" <an**@netcabo.pt.NOSPAM> scripsit:
This is the code I am using

<...>
Dim a As SizeTreeNode = HashTable.Item(path)
Me.TreeView1.SelectedNode = a
a.Parent.ExpandAll
<...>

Why doesn't this work? What am I doing wrong?
The node is neither selected nor expanded.


Set the treeview's 'HideSelection' property to 'False' to check if the
node is selected even if the treeview control doesn't have the focus.
If I do

MsgBox(a.Text)
MsgBox(Me.TreeView1.SelectedNode.Text)
I see that "a" is the correct node, but the selected node is the node
that
was selected prior to that statement. So what am I missing here?


Are you sure you placed this code after setting the 'SelectedNode'? To
check if the selected node has been set, use 'If
Me.TreeView1.SelectedNode Is a Then...' after setting the selected node.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>


Nov 21 '05 #9
I think I see the problem here.
I think the node in the Hashtable is just a copy (a clone) of the node on
the treeview. As such, the program sees the node on the treeview and the
node on the hashtable as two different nodes.
This can be verified if I do

Me.TreeView1.SelectedNode.Nodes.Add(a)

It will then add a copy of the node to the selected node, leaving that node
with two nodes which are the same.
Any other suggestions?

André Nogueira

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:e2**************@TK2MSFTNGP15.phx.gbl...
* "André Nogueira" <an**@netcabo.pt.NOSPAM> scripsit:
This is the code I am using

<...>
Dim a As SizeTreeNode = HashTable.Item(path)
Me.TreeView1.SelectedNode = a
a.Parent.ExpandAll
<...>

Why doesn't this work? What am I doing wrong?
The node is neither selected nor expanded.


Set the treeview's 'HideSelection' property to 'False' to check if the
node is selected even if the treeview control doesn't have the focus.
If I do

MsgBox(a.Text)
MsgBox(Me.TreeView1.SelectedNode.Text)
I see that "a" is the correct node, but the selected node is the node
that
was selected prior to that statement. So what am I missing here?


Are you sure you placed this code after setting the 'SelectedNode'? To
check if the selected node has been set, use 'If
Me.TreeView1.SelectedNode Is a Then...' after setting the selected node.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B URL:http://dotnet.mvps.org/dotnet/faqs/

Nov 21 '05 #10
Thank you! Your treeview works great.
One bug though:
If a node has in its path a path separator character, the property won't
work.
Example:
A treeview with
- C:\
----windows
----system32
would have the path C:\\windows\system32 which would cause your treeview to
cast an error.
This could be resolved by replacing every two Path separators with just one
until there are no more two path separator characters together. However,
this would not work if you have nodes with no text.
But this isn't a big problem to me, so... I'm good :)
Thanks again

André Nogueira

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uc**************@tk2msftngp13.phx.gbl...
André,
In addition to the other comments.

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 the other 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.

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
"André Nogueira" <an**@netcabo.pt.NOSPAM> wrote in message
news:ek**************@TK2MSFTNGP15.phx.gbl...
Hi there.
I know you can view a node's fullpath property, but is it posible to

select
a node using its path?
Like, tell the treeview that the node that should be selected is the node
with the path "Users\Administrators\John Doe"?
Thank you in advance!

Andre Nogueira


Nov 21 '05 #11
* "André Nogueira" <an**@netcabo.pt.NOSPAM> scripsit:
a = Me.TreeView1.SelectedNode.Parent
Me.TreeView1.SelectedNode = a
MsgBox(Me.TreeView1.SelectedNode Is a)
MsgBox(a.Text)
MsgBox(Me.TreeView1.SelectedNode.Text)

It works.
I did this to see if it was a problem with the selectednode property or
something. This sets the selected node to parent of the currently selected
node and it works.
This isn't what I want to do, this is just an experiment.
So what am I doing wrong with my other code?


It seems that there is a problem with 'ExpandAll', if I understand your
posts. Mhm, I don't have an idea...

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #12
* "André Nogueira" <an**@netcabo.pt.NOSPAM> scripsit:
I think I see the problem here.
I think the node in the Hashtable is just a copy (a clone) of the node on
the treeview. As such, the program sees the node on the treeview and the
node on the hashtable as two different nodes.
This can be verified if I do

Me.TreeView1.SelectedNode.Nodes.Add(a)

It will then add a copy of the node to the selected node, leaving that node
with two nodes which are the same.
Any other suggestions?


'TreeNode' is a class, and thus not a value type. So, a reference to
the same instance should be stored in the hashtable.

I suggest to turn 'Option Strict On', this will help you to identify
bugs in the code more easily. In your existing code, you will have to
change

\\\
Dim t As TreeNode = MyNodesHashtable.Item(Key)
///

to

\\\
Dim t As TreeNode = DirectCast(MyNodesHashtable.Item(Key), TreeNode)
///

This is not the reason why your code is not working, but it's at least a
starting point.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #13
I had already tried with CType, but to no avail.
Will try that now.

Andre Nogueira

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
* "André Nogueira" <an**@netcabo.pt.NOSPAM> scripsit:
I think I see the problem here.
I think the node in the Hashtable is just a copy (a clone) of the node on
the treeview. As such, the program sees the node on the treeview and the
node on the hashtable as two different nodes.
This can be verified if I do

Me.TreeView1.SelectedNode.Nodes.Add(a)

It will then add a copy of the node to the selected node, leaving that
node
with two nodes which are the same.
Any other suggestions?


'TreeNode' is a class, and thus not a value type. So, a reference to
the same instance should be stored in the hashtable.

I suggest to turn 'Option Strict On', this will help you to identify
bugs in the code more easily. In your existing code, you will have to
change

\\\
Dim t As TreeNode = MyNodesHashtable.Item(Key)
///

to

\\\
Dim t As TreeNode = DirectCast(MyNodesHashtable.Item(Key), TreeNode)
///

This is not the reason why your code is not working, but it's at least a
starting point.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ThunderMusic | last post by:
Hi, while I'm searching into a treeview to find a specific node, I find the code duplicating treeview nodes I just can't figure out what's causing this behavior, maybe some of you already...
4
by: Ian Powell | last post by:
Hi I've got objects in an sorted ArrayList like: P:\ P:\\DOCS P:\\i386 P:\\i386\ASMS P:\\i386\ASMS\1000 P:\\i386\ASMS\1000\MSFT
4
by: Karim El Jed | last post by:
Hi, I'm trying to expand a special Node of my TreeView from Codebehind. I have a TreeView on a page for navigating to another site. On the other tsite here is the same TreeView more precisely a...
12
by: Bob Hollness | last post by:
Hi. I have a text file containing a list of files and their path. I.e. c:\MyDocs\File1.txt c:\MyDocs\File2.txt c:\MyDocs\File3.txt c:\MyDocs\File4.txt C:\MyDocs\Folder\File1.txt
4
by: Andrew Robinson | last post by:
I am using a TreeView to perform navigation and have a few nodes that need to generate a popup menu. For this, I use the SelectedNodeChanged event and then add the relevant "window.open" script to...
10
by: p3t3r | last post by:
I have a treeview sourced from a SiteMap. I want to use 2 different CSS styles for the root level nodes. The topmost root node should not have a top border, all the other root nodes should have a...
4
by: praveen | last post by:
I have a form with treeview control loaded from xml document,text box, two buttons named "Find" and "FindNext" and my treeview which looks like below. Details |__ policy status |__ created by...
0
by: jiing | last post by:
Hi all, I want to use sting(the same as Node.Text) to judge if a node exists in TreeView. I've tried several ways, but seems all failed. could anybody help me? Thanks in advance. //My...
11
by: pbd22 | last post by:
Hi. I am getting odd treeview results and hope you can help. I am parsing a string, "x/y/z", turning it into an array (that always seems to start with an empty string) and then using the...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.