<p>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. </p>

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

[{'answer_id': 2546, 'author': 'Jonesinator', 'author_id': 152, 'author_profile': 'https://Stackoverflow.com/users/152', 'pm_score': 8, 'selected': True, 'text': '<p>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.</p>\n\n<pre><code>void treeView1MouseUp(object sender, MouseEventArgs e)\n{\n    if(e.Button == MouseButtons.Right)\n    {\n        // Select the clicked node\n        treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y);\n\n        if(treeView1.SelectedNode != null)\n        {\n            myContextMenuStrip.Show(treeView1, e.Location);\n        }\n    }\n}\n</code></pre>\n'}, {'answer_id': 5964, 'author': 'ICR', 'author_id': 214, 'author_profile': 'https://Stackoverflow.com/users/214', 'pm_score': 0, 'selected': False, 'text': "<p>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.</p>\n\n<p>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).</p>\n"}, {'answer_id': 360942, 'author': 'Marcus Erickson', 'author_id': 38373, 'author_profile': 'https://Stackoverflow.com/users/38373', 'pm_score': 4, 'selected': False, 'text': '<p>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.</p>\n\n<p>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.</p>\n\n<p>Here is some code that changes the selection during the right click:</p>\n\n<pre><code>  private void tree_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)\n  {\n     // only need to change selected note during right-click - otherwise tree does\n     // fine by itself\n     if ( e.Button == MouseButtons.Right )\n     {         \n        Point pt = new Point( e.X, e.Y );\n        tree.PointToClient( pt );\n\n        TreeNode Node = tree.GetNodeAt( pt );\n        if ( Node != null )\n        {\n           if ( Node.Bounds.Contains( pt ) )\n           {\n              tree.SelectedNode = Node;\n              ResetContextMenu();\n              contextMenuTree.Show( tree, pt );\n           }\n        }\n     }\n  }\n</code></pre>\n'}, {'answer_id': 4393244, 'author': 'deej', 'author_id': 311445, 'author_profile': 'https://Stackoverflow.com/users/311445', 'pm_score': 5, 'selected': False, 'text': '<p>Here is my solution. Put this line into NodeMouseClick event of the TreeView:</p>\n\n<pre><code> ((TreeView)sender).SelectedNode = e.Node;\n</code></pre>\n'}, {'answer_id': 13502892, 'author': 'Trevor Elliott', 'author_id': 852555, 'author_profile': 'https://Stackoverflow.com/users/852555', 'pm_score': 2, 'selected': False, 'text': "<p>Similar to Marcus' answer, this was the solution I found worked for me:</p>\n\n<pre><code>private void treeView_MouseClick(object sender, MouseEventArgs e)\n{\n    if (e.Button == MouseButtons.Right)\n    {\n        treeView.SelectedNode = treeView.GetNodeAt(e.Location);\n    }\n}\n</code></pre>\n\n<p>You need not show the context menu yourself if you set it to each individual node like so:</p>\n\n<pre><code>TreeNode node = new TreeNode();\nnode.ContextMenuStrip = contextMenu;\n</code></pre>\n\n<p>Then inside the ContextMenu's Opening event, the TreeView.SelectedNode property will reflect the correct node.</p>\n"}, {'answer_id': 14128111, 'author': 'Beta Carotin', 'author_id': 1593297, 'author_profile': 'https://Stackoverflow.com/users/1593297', 'pm_score': 4, 'selected': False, 'text': '<p>Reviving this question because I find this to be a much better solution.\nI use the <code>NodeMouseClick</code> event instead.</p>\n\n<pre><code>void treeview_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)\n{\n    if( e.Button == MouseButtons.Right )\n    {\n        tree.SelectedNode = e.Node;\n    }\n}\n</code></pre>\n'}, {'answer_id': 23943876, 'author': 'Ricky Manwell', 'author_id': 3689347, 'author_profile': 'https://Stackoverflow.com/users/3689347', 'pm_score': 0, 'selected': False, 'text': '<p>Here is how I do it.</p>\n\n<pre><code>private void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)\n{\n    if (e.Button == System.Windows.Forms.MouseButtons.Right)\n        e.Node.TreeView.SelectedNode = e.Node;\n}\n</code></pre>\n'}, {'answer_id': 41003526, 'author': 'George', 'author_id': 1876668, 'author_profile': 'https://Stackoverflow.com/users/1876668', 'pm_score': 2, 'selected': False, 'text': "<p>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:</p>\n\n<pre><code>// Global Private Variable to hold right-clicked Node\nprivate TreeNode _currentNode = new TreeNode();\n\n// Set Global Variable to the Node that was right-clicked\nprivate void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)\n{\n    if (e.Button == System.Windows.Forms.MouseButtons.Right)\n        _currentNode = e.Node;\n}\n\n// Do something when the Menu Item is clicked using the _currentNode\nprivate void toolStripMenuItem_Clicked(object sender, EventArgs e)\n{\n    if (_currentNode != null)\n        MessageBox.Show(_currentNode.Text);\n}\n</code></pre>\n"}, {'answer_id': 41109031, 'author': 'sparkyShorts', 'author_id': 2335488, 'author_profile': 'https://Stackoverflow.com/users/2335488', 'pm_score': 0, 'selected': False, 'text': "<p>Another option you could run with is to have a global variable that has the selected node. You would just need to use the <code>TreeNodeMouseClickEventArgs</code>.</p>\n\n<pre><code>public void treeNode_Click(object sender, TreeNodeMouseClickEventArgs e)\n{\n    _globalVariable = e.Node;\n}\n</code></pre>\n\n<p>Now you have access to that node and it's properties. </p>\n"}, {'answer_id': 61949562, 'author': 'AeonOfTime', 'author_id': 2298192, 'author_profile': 'https://Stackoverflow.com/users/2298192', 'pm_score': 0, 'selected': False, 'text': '<p>I would like to propose an alternative to using the click events, using the context menu\'s <code>Opened</code> event:</p>\n\n<pre class="lang-cs prettyprint-override"><code>private void Handle_ContextMenu_Opened(object sender, EventArgs e)\n{\n    TreeViewHitTestInfo info = treeview.HitTest(treeview.PointToClient(Cursor.Position));\n    TreeNode contextNode;\n\n    // was there a node where the context menu was opened?\n    if (info != null &amp;&amp; info.Node != null)\n    {\n        contextNode = info.Node;\n    }\n\n    // Set the enabled states of the context menu elements\n    menuEdit.Enabled = contextNode != null;\n    menuDelete.Enabled = contextNode != null;\n}\n</code></pre>\n\n<p>This has the following advantages that I can see:</p>\n\n<ul>\n<li>It does not change the selected node</li>\n<li>No separate event handler needed to store the target node instance</li>\n<li>Can disable menu items if the user right-clicks empty space in the TreeView</li>\n</ul>\n\n<p>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 <code>Opening</code> event instead. </p>\n'}]
['https://Stackoverflow.com/questions/2527', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/428/']