我已经有了一个带有几个treenodes的treeview。当我右键单击一个treenode时,我会得到一个带有不同选项的contextmenu ,例如'删除项目'。从contextmenu-item eventHandler获取treenode
是否有一种简单的方法可以在contextmenu-item的eventHandler中获取右键单击的treenode对象?
我已经有了一个带有几个treenodes的treeview。当我右键单击一个treenode时,我会得到一个带有不同选项的contextmenu ,例如'删除项目'。从contextmenu-item eventHandler获取treenode
是否有一种简单的方法可以在contextmenu-item的eventHandler中获取右键单击的treenode对象?
如果(右)点击一个节点,是不是成为选择的节点?
TreeNode needed = TreeViewX.SelectedNode;
干杯
我前一段时间也有类似的问题,我想出了这样的
解决方案创建自己的myContextMenuStrip类中你覆盖了OnItemClicked方法,它从标准的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);
}
}
派生单击特定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);
}
}
}
}
Thnx。这可能工作,但Valentijn的解决方案更明显:) –
嗯,是的,我过分复杂:) – erem
另一个想法是上下文菜单的标签属性设置为节点对象,然后从事件处理程序只是访问它。当然,这只有在你不使用标签的情况下才有效。
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
}
这个答案不适用于所有情况。如果右键单击未选中的节点,它将显示为选定节点。当菜单条状项目的事件处理程序触发时,TreeView将已将SelectedNode重置为之前的状态。 @ erem's是一个更准确的解决方案。 – Kleinux