Right click select on .Net TreeNode

前端 未结 3 2215
情歌与酒
情歌与酒 2020-12-30 22:28

I am trying to show a popup menu on my treeview when users right click - allowing them to choose context sensitive actions to apply against the selected node.

At the

相关标签:
3条回答
  • 2020-12-30 22:54

    Both left and right clicks fire a click event and cause the selection to change. However, in certain circumstances (that I haven't yet bothered to trace down) the selection will change from the node that was right clicked to the originally selected node.

    In order to make sure that the right click changes the selection, you can forcibly change the selected node by using the MouseDown event:

    treeView.MouseDown += (sender, args) =>
        treeView.SelectedNode = treeView.GetNodeAt(args.X, args.Y);
    

    A little better, as one of the other posters pointed out, is to use the NodeMouseClick event:

    treeView.NodeMouseClick += (sender, args) => treeView.SelectedNode = args.Node;
    
    0 讨论(0)
  • 2020-12-30 23:01

    yes. Here is processing for NodeMouseClick event:

    private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {
          treeView1.SelectedNode = e.Node;
    }
    
    0 讨论(0)
  • 2020-12-30 23:08

    Drag a context menu strip onto the form then:

     private void treeView1_MouseDown(object sender, MouseEventArgs e)
     {
       if (e.Button == MouseButtons.Right)
       {
           // Display context menu for eg:
           ContextMenu1.Show();
       }
    }
    
    0 讨论(0)
提交回复
热议问题