Search code examples
c#contextmenustrip

Which object was the chosen option from ContextMenuStrip


I make windows form application. I have on form TreeView, I add few nodes and add ContextMenuStrip.

  var menu = new ContextMenuStrip();
  menu.Items.Add("Some text", new Bitmap(1, 1), new EventHandler(function_name));

  var treeView = new TreeView(..);
  treeView.ContextMenuStrip = menu;      

  treeView.Nodes.Add(new TreeNode()
  {
         ...
         Tag = someObject
  });

My problems is how can I check in function function_name on which treeNode was clicked and chosen option from ContextMenuStrip

edit

function_name sygnature

 public void pokaz_DoubleClick(object sender, EventArgs e)
 {
 }

Solution

  • You can handle the TreeNodeMouseClick event. In your TreeNodeMouseClickEventHandler you will have access to a TreeNodeMouseClickEventArgs argument. This argument contains a number of properties you can use to check which mouse button was clicked on which node. For example.

    private TreeNode rightClickeNode;
    
    void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            rightClickedNode = e.Node;
        }
    }
    

    You can then access rightClickedNode from your function_name.