Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从contextmenu项eventHandler获取treenode_C#_Treeview_Contextmenu - Fatal编程技术网

C# 从contextmenu项eventHandler获取treenode

C# 从contextmenu项eventHandler获取treenode,c#,treeview,contextmenu,C#,Treeview,Contextmenu,我有一个有几个树节点的树视图。当我右键单击树节点时,我会看到一个上下文菜单 使用不同的选项,例如“删除项目” 有没有一种简单的方法可以在contextmenu项的eventHandler中获取右键单击的treenode对象 不久前我遇到了类似的问题,我想出了这样的解决方案 创建您自己的myContextMenuStrip类,该类派生自标准ContextMenuStrip public class myContextMenuStrip : ContextMenuStrip {

我有一个有几个树节点的树视图。当我右键单击树节点时,我会看到一个上下文菜单 使用不同的选项,例如“删除项目”


有没有一种简单的方法可以在contextmenu项的eventHandler中获取右键单击的treenode对象

不久前我遇到了类似的问题,我想出了这样的解决方案

创建您自己的myContextMenuStrip类,该类派生自标准ContextMenuStrip

    public class myContextMenuStrip : ContextMenuStrip
    {
        public TreeNode tn;

        public myContextMenuStrip() { }

        protected override void OnItemClicked(ToolStripItemClickedEventArgs e)
        {
            base.OnItemClicked(e);
            if (e.ClickedItem.Text == "asd") MessageBox.Show(tn.Text);
        }
    }
在内部,您正在重写OnItemClicked方法,当您单击特定菜单项时,该方法将显示MessageBox

所以,当您用鼠标右键单击treeView项时,它将从您的鼠标指针下检索节点并将其传递给您的myContextMenuStrip

    private void treeView1_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            TreeNode tn = treeView1.GetNodeAt(e.Location);
            myMenu.tn = tn;
        }
    }
在formLoad上,您正在初始化myContextMenuStrip,添加项目,并将其绑定到treeView

    private void Form1_Load(object sender, EventArgs e)
    {
        myMenu = new myContextMenuStrip();
        myMenu.Items.Add("asd");
        treeView1.ContextMenuStrip = myMenu;
    }
我知道这不是一种非常优雅的方式,但它很简单,而且很有效(与在EventArgs中传递treeNode值的想法相反,这可能很难实现,甚至是不可能实现的-我没有尝试过,尝试过几次,但还是放弃了)

整个工作代码,需要在表单上查看树视图:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public myContextMenuStrip myMenu;

    private void treeView1_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            TreeNode tn = treeView1.GetNodeAt(e.Location);
            myMenu.tn = tn;
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        myMenu = new myContextMenuStrip();
        myMenu.Items.Add("asd");
        treeView1.ContextMenuStrip = myMenu;
    }

    public class myContextMenuStrip : ContextMenuStrip
    {
        public TreeNode tn;

        public myContextMenuStrip() { }

        protected override void OnItemClicked(ToolStripItemClickedEventArgs e)
        {
            base.OnItemClicked(e);
            if (e.ClickedItem.Text == "asd") MessageBox.Show(tn.Text);
        }
    }
}
}
如果您(右)单击一个节点,它不是成为选定的节点吗

TreeNode needed = TreeViewX.SelectedNode;

Cheers

另一个想法是将上下文菜单的标记属性设置为节点对象,然后仅从事件处理程序访问它。当然,这只有在您不使用标签的情况下才有效

private void MyTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            MyContextMenu.Tag = e.Node;
            MyContextMenu.Show(this, e.Location);
        }
    }
private void MyToolStripMenuItem_Click(object sender, EventArgs e)
    {
        //Get TreeNode from Tag
        //Note: Could also get ContextMenu from sender,
        //but we already have it, so just access it directly
        TreeNode node = MyContextMenu.Tag as TreeNode;
        if (node == null)
            return;
        //Do stuff with node here
    }

Thnx。这可能有效,但瓦伦蒂金的解决方案更为明显:)好吧,是的,我把这个问题复杂化了:)这个答案不会在所有情况下都有效。如果在未选定的节点上单击鼠标右键,它将显示为选定节点。当菜单条项的事件处理程序触发时,TreeView将已将SelectedNode重置为以前的状态@erem是一个更准确的解决方案。为了避免@Kleinux所描述的情况,请记住将ContextMenuStrip附加到每个TreeNode,而不是附加到TreeView控件:var my_node=new TreeNode();my_node.ContextMenuStrip=my_菜单;