从contextmenu-item eventHandler获取treenode

时间:2012-12-23 17:14:34

标签: c# treeview contextmenu

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

是否有一种简单的方法可以在contextmenu-item的eventHandler中获取右键单击的treenode对象?

3 个答案:

答案 0 :(得分:6)

前段时间我遇到过类似的问题,我想出了这样的解决方案

创建自己的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方法,当你点击特定的menuItem时它会显示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值的想法相反,这可能难以实现甚至不可能 - 没有尝试自己,很少尝试但是已经辞职)

整个工作代码,需要表单上的treeView:

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);
        }
    }
}
}

答案 1 :(得分:1)

如果您(右)单击某个节点,它是不是成为所选节点?

TreeNode needed = TreeViewX.SelectedNode;

干杯

答案 2 :(得分:0)

另一个想法是将上下文菜单的Tag属性设置为节点对象,然后只从事件处理程序访问它。当然,这仅适用于您没有使用Tag的任何其他内容。

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
    }