# 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

• 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

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
Dim NewNode As TreeNode
Dim ChildNode As TreeNode

With TreeView1

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.

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

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
Dim NewNode As TreeNode
Dim ChildNode As TreeNode

With TreeView1

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.

Saturday, August 16, 2014 3:59 PM