C# 右键单击​​ TreeView 节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14208944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:03:31  来源:igfitidea点击:

C# Right click on TreeView nodes

c#winformstreeview

提问by DeathCoder

I have a TreeViewwith the parent node : Node0. I add 3 subnodes:

我有一个TreeView与父节点:Node0。我添加 3 subnodes

Node01
Node02
Node03

I have a popup menuthat is associate to each of the subnodes.

我有一个popup menu与每个子节点相关联的。

My problem: If I right-click directly to one of the subnodes, my popup does not display. So I have to Select the Subnode first and Right-click to have the popup displayed.

我的问题:如果我直接右键单击子节点之一,则不会显示弹出窗口。所以我必须先选择子节点并右键单击以显示弹出窗口。

  1. How can I change the code so that the Direct Right-Click on a specific SubNode open the PopupMenu?
  2. The popupMenu have only OpenMemenu in the list. When clicking on this menu, a windows is supposed to open and this windows should be associated to the submenu I have clicked. How to get the Event of the right-click submenu and display Form with it?
  1. 如何更改代码以便直接右键单击特定子节点打开弹出菜单?
  2. popupMenuOpenMe在列表中只有菜单。单击此菜单时,应打开一个窗口,并且此窗口应与我单击的子菜单相关联。如何获取右键子菜单的Event并用它显示Form?

EDIT:

Look at this

编辑:

看看这个

private void modifySettingsToolStripMenuItem_Click(object sender, EventArgs e)
    {
        try
        {
            String s = treeView1.SelectedNode.Text;
            new chartModify(s).ShowDialog();
        }
        catch (Exception er)
        {
            System.Console.WriteLine(">>>" + er.Message);
        }
    }

The line String s = treeView1.SelectedNode.Text;gets the name of the selected node and not the node that have been right-clicked.

So here I have to modify this piece of code with the

该行String s = treeView1.SelectedNode.Text;获取所选节点的名称,而不是已右键单击的节点。

所以在这里我必须用

private void treeview1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
                MessageBox.Show(e.Node.Name);
        }

I modify it like this:

我是这样修改的:

try
        {
            TreeNodeMouseClickEventArgs ee;
            new chartModify(ee.Node.Name).ShowDialog();
        }

but it does not work : Error:Use of unassigned local variable 'ee'

但它不起作用: Error:Use of unassigned local variable 'ee'

EDIT #2: Finaly got the solution

编辑#2:终于得到了解决方案

public string s;
private void modifySettingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                new chartModify(s).ShowDialog();
            }
            catch (Exception er)
            {
                System.Console.WriteLine(">>>" + er.Message);
            }
        }

and then

进而

private void treeview1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                s = e.Node.Name;
                menuStrip1.Show();
            }
        }

it works,
Thanks

它有效,
谢谢

采纳答案by Mark Hall

You can try using the NodeMouseClickEvent it uses the TreeNodeClickEventArgsto get the Button and the Node that was clicked.

您可以尝试使用NodeMouseClick它使用的事件TreeNodeClickEventArgs来获取按钮和被点击的节点。

private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
    if(e.Button == MouseButtons.Right)
        MessageBox.Show(e.Node.Name);
}


Modified Code to show Popup and created Form

修改代码以显示弹出窗口和创建的表单

public partial class Form1 : Form
{
    string clickedNode;
    MenuItem myMenuItem = new MenuItem("Show Me");
    ContextMenu mnu = new ContextMenu();
    public Form1()
    {
        InitializeComponent();
        mnu.MenuItems.Add(myMenuItem);
        myMenuItem.Click += new EventHandler(myMenuItem_Click);
    }

    void myMenuItem_Click(object sender, EventArgs e)
    {
        Form frm = new Form();
        frm.Text = clickedNode;
        frm.ShowDialog(this);
        clickedNode = "";
    }

    private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            clickedNode = e.Node.Name;
            mnu.Show(treeView1,e.Location);
        }
    }
}

回答by Jastill

This will give you the treenode at a particular mouse point when your right click.

当您右键单击时,这将为您提供特定鼠标点处的树节点。

if(e.Button == MouseButtons.Right)
        {
            TreeNode destinationNode = ((TreeView)sender).GetNodeAt(new Point(e.X, e.Y));
            //Do stuff
        }

From here you should be able to open a specific popup menu.

从这里您应该能够打开特定的弹出菜单。