3

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

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

4

3 回答 3

6

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

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

    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);
        }
    }
}
}
于 2012-12-23T20:34:53.683 回答
0

如果您(右键)单击一个节点,它不会成为选定节点吗?

TreeNode needed = TreeViewX.SelectedNode;

干杯

于 2012-12-25T11:01:38.320 回答
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
    }
于 2016-10-20T01:51:43.293 回答