Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Looping Through All Nodes In A VB.NET Tree View And Highlighting Them.

3,044 views
Skip to first unread message

Michael The Red

unread,
Jul 6, 2004, 12:31:10 AM7/6/04
to
I am attempting to loop through all the nodes in a tree view and
highlight them as the loop is active. All nodes would include to
children nodes of parent nodes and so on.

I have a function in my application that builds a tree view. It
populates the tree view with parent nodes. The parent nodes are the
drives on my pc. When the user selects the node, I have a function
that takes the tag of the node and passes it to the Directory method.
A node is then created under the selected node with folder names.

I now have a tree view with parent and children nodes. Now, I would
like to loop through all the nodes in the tree view as it is now. For
exampleůthe Root Parent nodes are drives A, C, D, E, and F. When the
user selects F, then children nodes are addedůlikeůMovie Folder, Doc
Folder, File Folderů.whatever folders that are under F. I would like
to loop through all the nodes and highlight them as the loop goes
along.

I figured I could get a node count in the beginning and simply
interate through the node index. In my example, the node count was
something like 15. so I figured I could just do something like thisů

Dim nodeCount As Intger = TreeView.GetNodeCount(True)
Dim inc As Integer = 0
Dim nodeTag As String

Do While Not(inc > nodeCount)
nodeTag = TreeView.Nodes.Item(inc).Tag
ĹHIGHLIGHT THIS NODE AND DO SOMETHING WITH NODE

inc = inc + 1
Loop

The problem I am seeing is that the node count is 15, because I added
nodes to the tree from a nodes selection event, but when I go to index
6, I get a message like "Index is out of range." I figured it should
be in range because the nodes exist.

Do I have to do something special to add the nodes to the nodes
collection? Aren't the nodes automatically added to the node
collections when they are created? Why would child nodes be out of
range?

I don't know how I can simply loop through all the nodes, could
someone please, please, please help me out with this?

Thank you all.

Herfried K. Wagner [MVP]

unread,
Jul 6, 2004, 6:11:14 AM7/6/04
to
* michae...@hotmail.com (Michael The Red) scripsit:

> I am attempting to loop through all the nodes in a tree view and
> highlight them as the loop is active. All nodes would include to
> children nodes of parent nodes and so on.

\\\
Private Sub RecurseNodes(ByVal col As TreeNodeCollection)
For Each tn As TreeNode In col
MsgBox(tn.Text)
If tn.Nodes.Count > 0 Then
RecurseNodes(tn.Nodes)
End If
Next tn
End Sub

Private Sub Test()
RecurseNodes(Me.TreeView1.Nodes)
End Sub
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Michael The Red

unread,
Jul 7, 2004, 9:25:30 PM7/7/04
to
Thank you so much for the help. I didn't think I would get the answer
to my question so quick, so kudos to your expertise.

Bye the way….do have any ideas on how I might be able to highlight the
current node as the loop executes?

Thanks once again for the prompt assistance!!!!!

Jhon

unread,
Jul 17, 2010, 1:33:04 PM7/17/10
to
man eres un moustro programando pusha
si que me ayudastes muxo haaaaaa

From http://www.developmentnow.com/g/39_2004_7_0_0_150781/Looping-Through-All-Nodes-In-A-VB-NET-Tree-View-And-Highlighting-Them-.htm

Posted via DevelopmentNow.com Groups
http://www.developmentnow.com/g/

0 new messages