有没有一种方法可以知道在左键单击其中一个 TreeNode 的 ContextMenuStrip 项目并且该项目的 CheckedState 发生变化后右键单击了哪个 TreeNode?
Is there a way to know which TreeNode is right clicked after one of its ContextMenuStrip items is left clicked and the item's CheckedState changes?
2018 年 5 月 10 日更新。 orhtej2 建议我的问题可能是 Determine what control the ContextMenuStrip was used on 的重复。这是 几乎 一个重复,但我的场景有一个显着差异。详情见我修改后的问题和我的回答。
请参阅下面的代码。我的目标是更清楚地确定在左键单击其 ContextMenuItems 之一后右键单击哪个 TreeNode。
现在,当我右键单击两个子节点之一时,TreeView1_NodeMouseClick
中的if
语句将单击的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
}
}
}
}
S.O 答案的 For a ContextMenuStrip
部分。问题 Determine what control the ContextMenuStrip was used on 几乎 回答了我的问题。
然而,在我的例子中,我想处理 ContextMenuStrip
项右键单击并在 CheckState
更改时访问 ContextMenuStrip
项的 CheckState
,所以我的代码使用 ContextMenuStrip
item _CheckStateChanged
事件方法而不是 ContextMenuStrip
item _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
}
}
}
2018 年 5 月 10 日更新。 orhtej2 建议我的问题可能是 Determine what control the ContextMenuStrip was used on 的重复。这是 几乎 一个重复,但我的场景有一个显着差异。详情见我修改后的问题和我的回答。
请参阅下面的代码。我的目标是更清楚地确定在左键单击其 ContextMenuItems 之一后右键单击哪个 TreeNode。
现在,当我右键单击两个子节点之一时,TreeView1_NodeMouseClick
中的if
语句将单击的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
}
}
}
}
S.O 答案的 For a ContextMenuStrip
部分。问题 Determine what control the ContextMenuStrip was used on 几乎 回答了我的问题。
然而,在我的例子中,我想处理 ContextMenuStrip
项右键单击并在 CheckState
更改时访问 ContextMenuStrip
项的 CheckState
,所以我的代码使用 ContextMenuStrip
item _CheckStateChanged
事件方法而不是 ContextMenuStrip
item _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
}
}
}