C# – How to check whether treeview root node has child nodes exists or not

ctreeviewwinforms

I have a treeview with a root node initially as Root. If i right click on Root node i will have a context menu displayed with some options as New and other if i select New i will add a child node to the Root node. If i again Right click on the Root node and if the Root has child nodes i would like to clear all child nodes and add a new child node how can i do this

Best Answer

TreeNode.Nodes gives you a list of all child nodes to the node you're looking at.

You can then call Clear on that collection to delete all childnodes.