Answered by:
Remove Empy Tree from TreeNode

Question
-
i have treenode that look like next text:
*1 **Ron **Dani **Mary **John *2 *3 **Sammit **Ram *4 *6 **Orit **Sami **Mor *7 *8 *9 *10
i want to remove all empty root nods ("2","4","7","8","9","10) and keep root with nods (in this case: "1","3,"6")
so i created loop for it:Public Sub RemoveMainEmptyNods() For Each Nod As TreeNode In TreeNode1.Nodes If Nod.Nodes.Count = 0 Then Nod.Remove() End If Next End Sub
for some reason, is not remove all empty root nodes. every time is keep the root "8", so this is the results that i get:
*1 **Ron **Dani **Mary **John *3 **Sammit **Ram *6 **Orit **Sami **Mor *8
when i run the loop again, i get the right result(see blow):
*1 **Ron **Dani **Mary **John *3 **Sammit **Ram *6 **Orit **Sami **Mor
so why is not work on the first time?
Saturday, August 16, 2014 10:59 AM
Answers
-
In the following treenode1 is a treeview.
Dim n As TreeNode = treenode1.Nodes(0).FirstNode Do While Not IsNothing(n) If n.Nodes.Count = 0 Then Dim nn As TreeNode = n.NextNode n.Remove() n = nn Else n = n.NextNode End If Loop
'Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it.' JohnWein
Multics
My Serial Port Answer
- Proposed as answer by tommytwotrain Saturday, August 16, 2014 4:05 PM
- Marked as answer by Leo (Apple) Yang Monday, August 25, 2014 2:52 AM
Saturday, August 16, 2014 11:37 AM -
The problem is when you remove a node the position of nod changes. If you step through your code one line at a time and look at the values of nod you will see.One solution is to jump out of the for and start the count again like this. I am sure there are better ways.
Public Class Form2 Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim NewNode As TreeNode Dim ChildNode As TreeNode With TreeView1 NewNode = .Nodes.Add("1") ChildNode = NewNode.Nodes.Add("Ron") ChildNode = NewNode.Nodes.Add("Dani") ChildNode = NewNode.Nodes.Add("Mary") ChildNode = NewNode.Nodes.Add("John") NewNode = .Nodes.Add("2") NewNode = .Nodes.Add("3") ChildNode = NewNode.Nodes.Add("Sammit") ChildNode = NewNode.Nodes.Add("Ram") NewNode = .Nodes.Add("4") NewNode = .Nodes.Add("6") ChildNode = NewNode.Nodes.Add("Orit") ChildNode = NewNode.Nodes.Add("Sami") ChildNode = NewNode.Nodes.Add("Mor") NewNode = .Nodes.Add("7") NewNode = .Nodes.Add("8") NewNode = .Nodes.Add("9") NewNode = .Nodes.Add("10") End With End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim exitflag As Boolean = True Do While exitflag exitflag = False For Each Nod As TreeNode In TreeView1.Nodes If Nod.Nodes.Count = 0 Then Nod.Remove() exitflag = True Exit For End If Next Loop End Sub End Class
PS I missed dbasnett's post for some reason. That example is probably better as it does not loop so much.
- Edited by tommytwotrain Saturday, August 16, 2014 4:07 PM
- Marked as answer by Leo (Apple) Yang Monday, August 25, 2014 2:52 AM
Saturday, August 16, 2014 3:59 PM
All replies
-
In the following treenode1 is a treeview.
Dim n As TreeNode = treenode1.Nodes(0).FirstNode Do While Not IsNothing(n) If n.Nodes.Count = 0 Then Dim nn As TreeNode = n.NextNode n.Remove() n = nn Else n = n.NextNode End If Loop
'Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it.' JohnWein
Multics
My Serial Port Answer
- Proposed as answer by tommytwotrain Saturday, August 16, 2014 4:05 PM
- Marked as answer by Leo (Apple) Yang Monday, August 25, 2014 2:52 AM
Saturday, August 16, 2014 11:37 AM -
The problem is when you remove a node the position of nod changes. If you step through your code one line at a time and look at the values of nod you will see.One solution is to jump out of the for and start the count again like this. I am sure there are better ways.
Public Class Form2 Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim NewNode As TreeNode Dim ChildNode As TreeNode With TreeView1 NewNode = .Nodes.Add("1") ChildNode = NewNode.Nodes.Add("Ron") ChildNode = NewNode.Nodes.Add("Dani") ChildNode = NewNode.Nodes.Add("Mary") ChildNode = NewNode.Nodes.Add("John") NewNode = .Nodes.Add("2") NewNode = .Nodes.Add("3") ChildNode = NewNode.Nodes.Add("Sammit") ChildNode = NewNode.Nodes.Add("Ram") NewNode = .Nodes.Add("4") NewNode = .Nodes.Add("6") ChildNode = NewNode.Nodes.Add("Orit") ChildNode = NewNode.Nodes.Add("Sami") ChildNode = NewNode.Nodes.Add("Mor") NewNode = .Nodes.Add("7") NewNode = .Nodes.Add("8") NewNode = .Nodes.Add("9") NewNode = .Nodes.Add("10") End With End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim exitflag As Boolean = True Do While exitflag exitflag = False For Each Nod As TreeNode In TreeView1.Nodes If Nod.Nodes.Count = 0 Then Nod.Remove() exitflag = True Exit For End If Next Loop End Sub End Class
PS I missed dbasnett's post for some reason. That example is probably better as it does not loop so much.
- Edited by tommytwotrain Saturday, August 16, 2014 4:07 PM
- Marked as answer by Leo (Apple) Yang Monday, August 25, 2014 2:52 AM
Saturday, August 16, 2014 3:59 PM