Treeview iteration
-
I have a tree created at design time with 3 root nodes and 3 levels 3x3x3 =39 nodes as the default.
The nodes can be dynamically recreated.
When I cleared the nodes then recreated them, I tried looping through to add properties to the nodes of each level including their relevant context menu name.
Private Sub RedoTree\_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles RedoTree.Click treeLoad() treeExecutive.ExpandAll() restoreTP() treeExecutive.Nodes(0).EnsureVisible() End Sub Sub restoreTP() ' renew treenode properties Dim s As String, l As Short treeExecutive.Update() For Each treenode In treeExecutive.Nodes s = treenode.Name l = Len(s) treenode.ForeColor = Color.GhostWhite If treenode.Text <> s Then treenode.ForeColor = Color.Black Select Case l Case 1 treenode.NodeFont = New Font("Arial Rounded MT Bold", 10.8) treenode.ContextMenuStrip = CMSRoot Case 2 treenode.NodeFont = New Font("Arial", 10.2) treenode.ContextMenuStrip = CMSTree Case Else treenode.NodeFont = New Font("Arial Narrow", 9) treenode.ContextMenuStrip = CMSTree End Select Next End Sub
The root nodes are number 1-3, then 11,12,13 .... 111,112,113 Total is 39 nodes
ONLY THE ROOT NODES ARE RECOGNISED AND DEALT WITH.
All the child nodes are ignored as if they don't exist
Am bogged down on this and would appreciate any help :confused:
Demac
-
I have a tree created at design time with 3 root nodes and 3 levels 3x3x3 =39 nodes as the default.
The nodes can be dynamically recreated.
When I cleared the nodes then recreated them, I tried looping through to add properties to the nodes of each level including their relevant context menu name.
Private Sub RedoTree\_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles RedoTree.Click treeLoad() treeExecutive.ExpandAll() restoreTP() treeExecutive.Nodes(0).EnsureVisible() End Sub Sub restoreTP() ' renew treenode properties Dim s As String, l As Short treeExecutive.Update() For Each treenode In treeExecutive.Nodes s = treenode.Name l = Len(s) treenode.ForeColor = Color.GhostWhite If treenode.Text <> s Then treenode.ForeColor = Color.Black Select Case l Case 1 treenode.NodeFont = New Font("Arial Rounded MT Bold", 10.8) treenode.ContextMenuStrip = CMSRoot Case 2 treenode.NodeFont = New Font("Arial", 10.2) treenode.ContextMenuStrip = CMSTree Case Else treenode.NodeFont = New Font("Arial Narrow", 9) treenode.ContextMenuStrip = CMSTree End Select Next End Sub
The root nodes are number 1-3, then 11,12,13 .... 111,112,113 Total is 39 nodes
ONLY THE ROOT NODES ARE RECOGNISED AND DEALT WITH.
All the child nodes are ignored as if they don't exist
Am bogged down on this and would appreciate any help :confused:
Demac
The TreeView's Nodes property[^] only returns the root nodes. Each node has its own Nodes property[^] containing its child nodes. To iterate through all nodes in the tree, you will need a recursive method. For example:
Sub restoreTP()
treeExecutive.BeginUpdate()
Try
restoreTP(treeExecutive.Nodes)
Finally
treeExecutive.EndUpdate()
End Try
End SubSub restoreTP(ByVal nodes As TreeNodeCollection)
Dim s As String, l As Integer
For Each treenode As TreeNode in nodes
...restoreTP(treenode.Nodes) Next
End Sub
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
The TreeView's Nodes property[^] only returns the root nodes. Each node has its own Nodes property[^] containing its child nodes. To iterate through all nodes in the tree, you will need a recursive method. For example:
Sub restoreTP()
treeExecutive.BeginUpdate()
Try
restoreTP(treeExecutive.Nodes)
Finally
treeExecutive.EndUpdate()
End Try
End SubSub restoreTP(ByVal nodes As TreeNodeCollection)
Dim s As String, l As Integer
For Each treenode As TreeNode in nodes
...restoreTP(treenode.Nodes) Next
End Sub
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Hi thx Richard for that info Not familiar with recursion, but now you've helped me improve my understanding of treeviews. I did circumvent the problem by updating node properties as they are created. I can post that code if it might help others. demac ;)