如何检测Treeview是否具有TreeNodes(VB.Net/C#)

时间:2015-07-07 19:55:54

标签: vb.net treeview treenode

我怎么知道TreeView是否有TreeNodes,无论它们是父节点还是子节点?我只是想避免重复。伪代码如下:

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

1 个答案:

答案 0 :(得分:2)

您可以检查TreeView1.Nodes.Count,或者只需调用TreeView1.Nodes.Clear()以确保在添加新的之前它始终清除

更新以获得更完整的答案:

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