简体   繁体   English

右键单击TreeNode以选择不起作用

[英]TreeNode right click to select not working

I am trying to fix a problem where if a user right clicks on a selection, it will not select/highlight it, and if, for example. 我正在尝试解决一个问题,例如,如果用户右键单击某个选择,它将不会选择/突出显示该选择,例如。 "delete", is selected, it deletes the previous selection that was clicked on. 选择“删除”,它将删除之前单击的选择。 I have read many posts about using the mouse_down event, but nothing I have tried has seemed to work for me. 我已经阅读了许多有关使用mouse_down事件的文章,但是我尝试过的任何事情似乎都对我没有用。 Here is the current code: 这是当前代码:

private void treelocations_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Right)
    {
        Point pos = new Point();
        pos.X = e.X;
        pos.Y = e.Y;
        mnulocation.Show(this.treelocations, pos);
    }
}

I simply would like to be able to right click on any selection in the list and have it highlight/select that record. 我只是希望能够右键单击列表中的任何选择,并使其突出显示/选择该记录。

You will need to change the selected node upon firing the mouse down event. 触发鼠标按下事件时,您将需要更改选定的节点。

Take a look at: Selected Node Property 看一下: 选定的节点属性

Or your could take a look at: NodeMouseClick event. 或者您可以看一下: NodeMouseClick事件。

    private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            treeView1.Nodes.Remove(e.Node);
        }
    }

You can add in the other case statement as needed but this should help you get what you need 您可以根据需要添加其他case语句,但这应该可以帮助您获得所需的内容

 private void treelocations_MouseClick(object sender, MouseEventArgs e)
 {
      switch(e.Button)
      {
          case MouseButtons.Right:
          {
              Point pos = new Point();
              pos.X = e.X;
              pos.Y = e.Y;
              treeView1.Focus();
              MessageBox.Show(treelocations.SelectedNode.Text);
              break;
           }
      }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM