有没有办法知道在左键单击其中一个ContextMenuStrip项目并且项目的CheckedState发生变化后右键单击了哪个TreeNode?

时间:2018-05-09 19:05:40

标签: c# winforms treeview contextmenustrip toolstripmenu

于5/10/2018更新。 orhtej2建议我的问题可能是Determine what control the ContextMenuStrip was used on的重复。它是几乎一个重复,但我的方案有一个显着的区别。有关详细信息,请参阅我修改过的问题和我的答案。

请参阅下面的代码。我的目标是更清楚地确定在单击其中一个ContextMenuItem之后右键单击哪个TreeNode。

就像现在一样,当我右键单击两个子节点之一时,if中的TreeView1_NodeMouseClick语句会将点击的TreeNode加载到全局treeViewClickedNodeTreeNode对象。然后,当我左键单击两个contextMenuStripChildNode ToolStripMenuItem中的一个时,会触发DocumentActionToolStripMenuItem_CheckStateChanged方法。然后我可以检查检查状态。如果已选中,我可以对treeViewClickedNode TreeNode执行某些操作。

我的问题:在左键单击其中一个ContextMenuStrip项后,有没有更清晰的方法来确定右键单击哪个TreeNode,即有没有办法取消全局变量treeViewClickedNode

注意:我在设计师中唯一做的就是将treeview1放在Form1上,将其停靠在Form1并设置' treeview1' NodeMouseClickTreeView1_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
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

S.O.答案的For a ContextMenuStrip部分问题Determine what control the ContextMenuStrip was used on 几乎回答了我的问题。

但是,在我的情况下,我想要处理ContextMenuStrip项右键单击并在ContextMenuStrip更改时访问CheckStateCheckState,因此我的代码使用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
        }
    }

}