于5/10/2018更新。 orhtej2建议我的问题可能是Determine what control the ContextMenuStrip was used on的重复。它是几乎一个重复,但我的方案有一个显着的区别。有关详细信息,请参阅我修改过的问题和我的答案。
请参阅下面的代码。我的目标是更清楚地确定在单击其中一个ContextMenuItem之后右键单击哪个TreeNode。
就像现在一样,当我右键单击两个子节点之一时,if
中的TreeView1_NodeMouseClick
语句会将点击的TreeNode
加载到全局treeViewClickedNode
中TreeNode
对象。然后,当我左键单击两个contextMenuStripChildNode
ToolStripMenuItem
中的一个时,会触发DocumentActionToolStripMenuItem_CheckStateChanged
方法。然后我可以检查检查状态。如果已选中,我可以对treeViewClickedNode
TreeNode
执行某些操作。
我的问题:在左键单击其中一个ContextMenuStrip项后,有没有更清晰的方法来确定右键单击哪个TreeNode,即有没有办法取消全局变量treeViewClickedNode
?
注意:我在设计师中唯一做的就是将treeview1
放在Form1
上,将其停靠在Form1
并设置' treeview1' NodeMouseClick
至TreeView1_NodeMouseClick
using System;
using System.Windows.Forms;
namespace WindowsFormsApp_Scratch
{
public partial class Form1 : Form
{
TreeNode treeViewClickedNode;
ContextMenu mnu = new ContextMenu();
public Form1()
{
InitializeComponent();
// Create the root node.
TreeNode treeNodeRoot = new TreeNode("Documents");
// Add the root node to the TreeView.
treeView1.Nodes.Add(treeNodeRoot);
//Create and add child 2 nodes each with a two item ContextMenuStrip.
string[] childNodeLabels = { "document1.docx", "document2.docx"};
string[] contextItemLabels = { "Action A", "Action B" };
foreach (String childNodeLabel in childNodeLabels)
{
TreeNode treeNode = treeNodeRoot.Nodes.Add(childNodeLabel);
// Create a ContextMenuStrip for this child node.
ContextMenuStrip contextMenuStripChildNode = new ContextMenuStrip
{
ShowCheckMargin = true,
ShowImageMargin = false
};
foreach (String contextItemLabel in contextItemLabels)
{
//Create a menu item.
ToolStripMenuItem action = new ToolStripMenuItem(contextItemLabel, null, DocumentActionToolStripMenuItem_CheckStateChanged)
{
CheckOnClick = true
};
contextMenuStripChildNode.Items.Add(action);
}
treeNode.ContextMenuStrip = contextMenuStripChildNode;
}
private void TreeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
TreeView t = (TreeView)sender;
//Force the node that was right-clicked to be selected
t.SelectedNode = t.GetNodeAt(e.X, e.Y);
if (e.Button == MouseButtons.Right)
{
treeViewClickedNode = e.Node;
}
}
private void DocumentActionToolStripMenuItem_CheckStateChanged(object sender, EventArgs e)
{
ToolStripMenuItem toolStripMenuItem = (ToolStripMenuItem)sender;
if (toolStripMenuItem.CheckState == CheckState.Checked)
{
//Do something with treeViewClickedNode object
}
}
}
}
答案 0 :(得分:0)
S.O.答案的For a ContextMenuStrip
部分问题Determine what control the ContextMenuStrip was used on 几乎回答了我的问题。
但是,在我的情况下,我想要处理ContextMenuStrip
项右键单击并在ContextMenuStrip
更改时访问CheckState
项CheckState
,因此我的代码使用ContextMenuStrip
项_CheckStateChanged
事件方法,而不是ContextMenuStrip
项_Click
事件方法。因此,我需要将发件人转换为ToolStripMenuItem
而不是ToolStripItem
。除此之外,我使用S.O.的答案的For a ContextMenuStrip
部分。我的DocumentActionToolStripMenuItem_CheckStateChanged
事件方法中的问题Determine what control the ContextMenuStrip was used on:
private void DocumentActionToolStripMenuItem_CheckStateChanged(object sender, EventArgs e)
{
Control treeNodeControl;
ToolStripMenuItem toolStripMenuItem = (ToolStripMenuItem)sender;
// if the ToolStripMenuItem item is owned by a ContextMenuStrip ...
if (toolStripMenuItem.Owner is ContextMenuStrip contextMenuStrip)
{
// Get the TreeNode that is displaying this context menu
treeNodeControl = contextMenuStrip.SourceControl;
if (toolStripMenuItem.CheckState == CheckState.Checked)
{
//Do something with treeNodeControl.SelectedNode treeView node
}
}
}