codelearner
codelearner

Reputation: 25

How to detect if Treeview has TreeNodes or not (VB.Net/C#)

How can I know if a TreeView has TreeNodes, regardless of whether they are parent or child nodes? I just want to avoid duplication. The pseudo-code looks like:

If TreeView1 has TreeNodes Then   
   'Remove old ones and create another   
   'Or Exit to skep creation of new ones  
Else  
   'Create TreeNodes  
End Sub 

Upvotes: 0

Views: 220

Answers (1)

oppassum
oppassum

Reputation: 1765

You can check TreeView1.Nodes.Count or you can simply call TreeView1.Nodes.Clear() to ensure it's always clear before you add new ones

Update for more complete answer:

If TreeView1.Nodes.Count = 0 Then   
   'Remove old ones and create another   
   'Or Exit to skep creation of new ones  
Else  
   'Create TreeNodes  
End Sub 

Upvotes: 2

Related Questions