Click here to Skip to main content
15,881,092 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
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
Posted
Comments
OriginalGriff 23-Sep-10 7:53am    
"But the node count will always be 1 na so how can i know whether child nodes exists or not - Dorababu743 10 mins ago"
Cobblers! Try it:
            TreeView tv = new TreeView();
            TreeNode n = new TreeNode();
            Console.WriteLine("Roots: " + tv.Nodes.Count);
            tv.Nodes.Add(n);
            n = new TreeNode();
            Console.WriteLine("Roots: " + tv.Nodes.Count);
            tv.Nodes.Add(n);
            Console.WriteLine("Roots: " + tv.Nodes.Count);
            TreeNode sn = new TreeNode();
            Console.WriteLine("SubNodes: " + n.Nodes.Count);
            n.Nodes.Add(sn);
            Console.WriteLine("SubNodes: " + n.Nodes.Count);
            sn = new TreeNode();
            n.Nodes.Add(sn);
            Console.WriteLine("SubNodes: " + n.Nodes.Count);

1 solution

Look at:
TreeNode.Nodes.Add(childNode)
TreeNode.Nodes.Clear()
TreeNode.Nodes.Count

All available on MSDN via Google.
 
Share this answer
 
Comments
demouser743 23-Sep-10 7:41am    
But the node count will always be 1 na so how can i know whether child nodes exists or not
demouser743 23-Sep-10 7:41am    
I know how to add child nodes but the one i asked is different

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900