views:

131

answers:

5

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

+4  A: 

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.

ho1
But how can i check for child node exists for the Root i am unable to get it
Dorababu
@dorababu: You use the location from the `MouseDown` event together with a `HitTest` call. See my answer to this question for code to do that: http://stackoverflow.com/questions/3760256/changing-the-node-value-of-treeview/3760405#3760405
ho1
Actually my issue is different i only asked that question that works but i am not able to get the child count of a treeview
Dorababu
@dorababu: Sorry, I didn't realise that that was your question as well. I'm not sure if I understand your question then. If you always just want to have the first root node (and you know that the treeview will always have at least one node) you can just call `myTreeView.Nodes[0]` which will give you a `TreeNode` which will have it's own `Nodes` collection.
ho1
It's the `TreeView.Nodes` property as described here: http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.nodes.aspx
ho1
@Ho1 : Actually when my form was initially loaded i will have a treeview with only a Root node. If i right click on that i will have a context menu where i will add a child from the option NEW selected by the user. Now my treeview will have a Root node and child node. Again if i right click on the root node and select an NEW from context menu i would like to clear the child node that exists and i would like to add a new child node to the treeview.
Dorababu
@dorababu: If you take this line `info.Node.Text = "new path";` from my answer to your previous question and replace that with the following two lines `info.Node.Nodes.Clear();` and `info.Node.Nodes.Add(newNode);`. Does that give you what you want?
ho1
A: 

In the 'right click' handler, assuming you use Mouse Click, you can use the event args TreeNodeMouseClickEventArgs to get the current node ...

void tv_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { if (e.Button == MouseButtons.Right) { TreeNode selectedNode = e.Node; } }

Rob Lowther
A: 

You can check TreeNode.Nodes property. If count is greater than zero then you have child nodes, otherwise not

B-Rain
A: 

I write as follows

     TreeNode tn = new TreeNode();
        if (tn.Nodes.Count > 0)

But even if tree has child nodes i am not getting the count value to 1

Dorababu
I didn't see this before, it's better if you make comments rather than answers and/or amend your question. The mistake you're doing above is that you're creating a new node, you have to get out the existing one. So assuming you already have the saved location from the `MouseDown` you can do: `if(treeView1.HitTest(location).Node.Nodes.Count > 0)`
ho1
This gives me the solution
Dorababu
But how can i remove my child nodes with keeping the root node as same
Dorababu
A: 

After all the final answer is as follows

    if (tvwACH.HitTest(location).Node.Nodes.Count > 0 && tvwACH.SelectedNode.Parent == null )
        {
            foreach (TreeNode node in tvwACH.Nodes)
            {
                node.Nodes.Clear();
            }
    }
Dorababu