Question

I have TreeView with different level TreeNodes
I added same ContextMenuStrip to all parent TreeNodes and i want to get possibility to delete all child TreeNodes by opening that ContextMenuStrip and pressing "Delete all"

private void btn_delete_all_objects_Click(object sender, EventArgs e)
{
    ToolStripMenuItem tsmi = (ToolStripMenuItem)sender; //that way i receive button "Delete all"
    ContextMenuStrip cms = (ContextMenuStrip)tsmi.Owner; //this is ContextMenuStrip where this button...
    TreeView tw = (TreeView)cms.SourceControl; //i can get TreeView :( BUT I NEED TreeNode!
    TreeNode tn = tw.SelectedNode; //bah... if i select some of child nodes, then right click to open menu on parent, selected node is still that child
}

And i don't know how to get that TreeNode where user clicked to open menu
Any ideas?

Was it helpful?

Solution

you can use HitTest() method from tree view to find the node, like this,

var hitTest = treeView1.HitTest(treeView1.PointToClient(new Point(contextMenuStrip1.Left, contextMenuStrip1.Top)));
if (hitTest.Node != null)
{
    // Place your code to delete nodes
}

or you can focus the node on which the mouse clicked and use treeView1.SelectedNode property to manipulate in your menu items. By this way you can avoid using HitTest() on every context menu item...

private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
    var hitTest = treeView1.HitTest(treeView1.PointToClient(e.Location));
    if (hitTest.Node != null)
    {
         treeView1.SelectedNode = hitTest.Node;
    }
}

OTHER TIPS

It is not clear what you are trying to do with the code above. But to do what you require (i.e. right click on TreeNode and launch a specific ContextMenuStrip) use code simalar to

private void customTreeViewSql_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
    // Ensure selected node is selected.
    treeView.BeginUpdate();
    this.treeView.SelectedNode = e.Node;
    try
    {
        if (e.Node.Level == 0)
        {
            if (e.Button == MouseButtons.Right)
                contextMenuStripA.Show(MousePosition);
        }
        else if (e.Node.Level == 1)
        {
            if (e.Button == MouseButtons.Right)
                contextMenuStripB.Show(MousePosition);
        }
    }
    finally
    {
        this.treeView.EndUpdate();
    }
}

Then you can setup the click event in the ContexMenu to delete all child nodes based on that selected

private void toolStripMenuDeleteAll_Click(object sender, EventArgs e)
{
    TreeNode treeNode = this.treeView.SelectedNode;
    foreach (TreeNode n in treeNode.Nodes)
        n.Remove();
}

I hope this helps.

i think this is much clear and easy than Krishnakumar's code. you can set your 'toolStripMenuDeleteAll' to some treenodes only and not to all of them. this code don't change the Selected TreeNode At all.

welcome CHADASH

TreeNode LastClickedTreeNode;
private void customTreeViewSql_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
    // update last treenode clicked.
    LastClickedTreeNode = e.Node;
}

private void toolStripMenuDeleteAll_Click(object sender, EventArgs e)
{
    foreach (TreeNode n in LastClickedTreeNode.Nodes)
        n.Remove();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top