76

How can I find out which node in a tree list the context menu has been activated? For instance right-clicking a node and selecting an option from the menu.

I can't use the TreeViews' SelectedNode property because the node is only been right-clicked and not selected.

10 Answers 10

97

You can add a mouse click event to the TreeView, then select the correct node using GetNodeAt given the mouse coordinates provided by the MouseEventArgs.

void treeView1MouseUp(object sender, MouseEventArgs e)
{
    if(e.Button == MouseButtons.Right)
    {
        // Select the clicked node
        treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y);

        if(treeView1.SelectedNode != null)
        {
            myContextMenuStrip.Show(treeView1, e.Location);
        }
    }
}
2
  • 8
    Thank you very much, I have a small addition to solution: You could also use the "_NodeMouseClick" event which gives you a "TreeNodeMouseClickEventArgs e" in which case you can just use the e.Node and don't have to bother with checking wether the node is null or not.
    – SDM
    Jul 9, 2013 at 14:44
  • I got the desired result with the MouseDown event instead of MouseUp.
    – Javier
    Jul 31, 2013 at 12:53
22

Here is my solution. Put this line into NodeMouseClick event of the TreeView:

 ((TreeView)sender).SelectedNode = e.Node;
2
  • The mouse event args doesn't have .Node
    – CAD bloke
    Jan 13, 2012 at 2:04
  • 3
    Yeah but TreeNodeMouseClickEventArgs (which are passed into NodeMouseClick event) do. So my solution is working and not overcomplicated like some other solutions
    – deej
    Jun 21, 2012 at 18:17
12

I find the standard windows treeview behavior selection behavior to be quite annoying. For example, if you are using Explorer and right click on a node and hit Properties, it highlights the node and shows the properties dialog for the node you clicked on. But when you return from the dialog, the highlighted node was the node previously selected/highlighted before you did the right-click. I find this causes usability problems because I am forever being confused on whether I acted on the right node.

So in many of our GUIs, we change the selected tree node on a right-click so that there is no confusion. This may not be the same as a standard iwndos app like Explorer (and I tend to strongly model our GUI behavior after standard window apps for usabiltiy reasons), I believe that this one exception case results in far more usable trees.

Here is some code that changes the selection during the right click:

  private void tree_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
  {
     // only need to change selected note during right-click - otherwise tree does
     // fine by itself
     if ( e.Button == MouseButtons.Right )
     {         
        Point pt = new Point( e.X, e.Y );
        tree.PointToClient( pt );

        TreeNode Node = tree.GetNodeAt( pt );
        if ( Node != null )
        {
           if ( Node.Bounds.Contains( pt ) )
           {
              tree.SelectedNode = Node;
              ResetContextMenu();
              contextMenuTree.Show( tree, pt );
           }
        }
     }
  }
1
  • This is the best solution for me because it actually selects the node you clicked on.
    – CAD bloke
    Jan 13, 2012 at 2:07
11

Reviving this question because I find this to be a much better solution. I use the NodeMouseClick event instead.

void treeview_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
    if( e.Button == MouseButtons.Right )
    {
        tree.SelectedNode = e.Node;
    }
}
1
  • I like this much better as well.
    – Joey Novak
    Jan 31, 2017 at 22:39
3

This is a very old question, but I still found it useful. I am using a combination of some of the answers above, because I don't want the right-clicked node to become the selectedNode. If I have the root node selected and want to delete one of it's children, I don't want the child selected when I delete it (I am also doing some work on the selectedNode that I don't want to happen on a right-click). Here is my contribution:

// Global Private Variable to hold right-clicked Node
private TreeNode _currentNode = new TreeNode();

// Set Global Variable to the Node that was right-clicked
private void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Right)
        _currentNode = e.Node;
}

// Do something when the Menu Item is clicked using the _currentNode
private void toolStripMenuItem_Clicked(object sender, EventArgs e)
{
    if (_currentNode != null)
        MessageBox.Show(_currentNode.Text);
}
2

Similar to Marcus' answer, this was the solution I found worked for me:

private void treeView_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        treeView.SelectedNode = treeView.GetNodeAt(e.Location);
    }
}

You need not show the context menu yourself if you set it to each individual node like so:

TreeNode node = new TreeNode();
node.ContextMenuStrip = contextMenu;

Then inside the ContextMenu's Opening event, the TreeView.SelectedNode property will reflect the correct node.

0

If you want the context menu to be dependent on the selected item you're best move I think is to use Jonesinator's code to select the clicked item. Your context menu content can then be dependent on the selected item.

Selecting the item first as opposed to just using it for the context menu gives a few advantages. The first is that the user has a visual indication as to which he clicked and thus which item the menu is associated with. The second is that this way it's a hell of a lot easier to keep compatible with other methods of invoking the context menu (e.g. keyboard shortcuts).

0

Here is how I do it.

private void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Right)
        e.Node.TreeView.SelectedNode = e.Node;
}
0

Another option you could run with is to have a global variable that has the selected node. You would just need to use the TreeNodeMouseClickEventArgs.

public void treeNode_Click(object sender, TreeNodeMouseClickEventArgs e)
{
    _globalVariable = e.Node;
}

Now you have access to that node and it's properties.

0

I would like to propose an alternative to using the click events, using the context menu's Opened event:

private void Handle_ContextMenu_Opened(object sender, EventArgs e)
{
    TreeViewHitTestInfo info = treeview.HitTest(treeview.PointToClient(Cursor.Position));
    TreeNode contextNode;

    // was there a node where the context menu was opened?
    if (info != null && info.Node != null)
    {
        contextNode = info.Node;
    }

    // Set the enabled states of the context menu elements
    menuEdit.Enabled = contextNode != null;
    menuDelete.Enabled = contextNode != null;
}

This has the following advantages that I can see:

  • It does not change the selected node
  • No separate event handler needed to store the target node instance
  • Can disable menu items if the user right-clicks empty space in the TreeView

Note: if you worry that the user may have already moved the mouse by the time the menu is opened, it is possible to use the Opening event instead.

Not the answer you're looking for? Browse other questions tagged or ask your own question.