How can I find out which node in a tree list the context menu has been activated for. i.e. right-clicking a node and selecting an option from the menu. I can't use the TreeViews' SelectedNode because the its only been right-clicked and not selected.

+23  A: 

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)

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).

+1  A: 

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;
              contextMenuTree.Show( tree, pt );
Marcus Erickson