Find node clicked under context menu

前端 未结 10 1509
故里飘歌
故里飘歌 2020-11-30 21:49

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

10条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 22:23

    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);
    }
    

提交回复
热议问题