C# ContextMenu将不会保持打开状态。Net 4.0客户端配置文件WinForms

C# ContextMenu将不会保持打开状态。Net 4.0客户端配置文件WinForms,c#,winforms,.net-4.0,c#-4.0,C#,Winforms,.net 4.0,C# 4.0,在处理超过第一级的单击事件后,我无法确定如何保持上下文菜单打开。这里是一个例子,我有一个上下文菜单和一个可检查菜单的菜单。我在处理单击事件后打开上下文菜单,但必须手动返回内部菜单。有没有办法以编程方式打开外部菜单或防止内部菜单关闭 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Lin

在处理超过第一级的单击事件后,我无法确定如何保持上下文菜单打开。这里是一个例子,我有一个上下文菜单和一个可检查菜单的菜单。我在处理单击事件后打开上下文菜单,但必须手动返回内部菜单。有没有办法以编程方式打开外部菜单或防止内部菜单关闭

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 NoCloseContextMenu
{
    public partial class Form1 : Form
    {
        bool[] store_checks = new bool[8];

        public Form1()
        {
            InitializeComponent();
            richTextBox1.AppendText("http://");
            richTextBox1.LinkClicked += new LinkClickedEventHandler(richTextBox1_LinkClicked);
        }

        void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
        {
            MenuItemExtended[] inner_menuitems = new MenuItemExtended[8];
            for (int i = 0; i < store_checks.Length; i++)
            {
                MenuItemExtended inner_menuitem = new MenuItemExtended("Check #" + i.ToString());
                inner_menuitem.menuitem_index = i;
                inner_menuitem.contextmenu_point = this.PointToClient(Cursor.Position);
                inner_menuitem.Checked = store_checks[i];
                inner_menuitem.Shortcut = (Shortcut)(131120 + i); //Ctrl+i = 131120+i
                inner_menuitem.ShowShortcut = true;
                inner_menuitem.Click += new EventHandler(inner_menuitem_Click);
                inner_menuitems[i] = inner_menuitem;
            }
            MenuItem outer_menu = new MenuItem("Outer Menu", inner_menuitems);
            ContextMenu context_menu = new ContextMenu(new MenuItem[] { outer_menu });
            context_menu.Show(this, this.PointToClient(Cursor.Position));
        }

        void inner_menuitem_Click(object sender, EventArgs e)
        {
            MenuItemExtended sender_menu = (MenuItemExtended)sender;
            store_checks[sender_menu.menuitem_index] = !store_checks[sender_menu.menuitem_index];
            sender_menu.Checked = !sender_menu.Checked;
            sender_menu.GetContextMenu().Show(this, sender_menu.contextmenu_point);
        }

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.richTextBox1 = new System.Windows.Forms.RichTextBox();
            this.SuspendLayout();
            // 
            // richTextBox1
            // 
            this.richTextBox1.Location = new System.Drawing.Point(13, 13);
            this.richTextBox1.Name = "richTextBox1";
            this.richTextBox1.Size = new System.Drawing.Size(100, 96);
            this.richTextBox1.TabIndex = 0;
            this.richTextBox1.Text = "";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.richTextBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.RichTextBox richTextBox1;
    }

    public class MenuItemExtended : MenuItem
    {
        public int menuitem_index;
        public Point contextmenu_point;

        public MenuItemExtended(string text)
        {
            this.Text = text;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
命名空间NoCloseContextMenu
{
公共部分类Form1:Form
{
bool[]商店检查=新bool[8];
公共表格1()
{
初始化组件();
richTextBox1.AppendText(“http://”);
richTextBox1.LinkClicked+=新链接clickedEventHandler(richTextBox1\u LinkClicked);
}
void richTextBox1_LinkClicked(对象发送者,LinkClickedEventArgs e)
{
MenuItemExtended[]内部\u menuitems=新MenuItemExtended[8];
for(int i=0;i

还有,有没有办法让“Control+number”快捷方式工作并激活click事件?提前感谢您的帮助

我强烈建议不要处理“父”上下文菜单项上的单击事件-让操作系统为您处理。

我没有找到任何方法阻止上下文菜单关闭,因此我使用了ContextMenuStrip和ToolStripMenuItem。这也解决了我以前没有使用快捷方式的问题。我处理包含可检查项的菜单的关闭事件,并在单击/检查项时取消关闭

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 NoCloseContextMenu
{
    public partial class Form1 : Form
    {
        bool[] store_checks = new bool[8];

        public Form1()
        {
            InitializeComponent();
            richTextBox1.AppendText("http://");
            richTextBox1.LinkClicked += new LinkClickedEventHandler(richTextBox1_LinkClicked);
        }

        void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
        {
            ToolStripMenuItem[] inner_menuitems = new ToolStripMenuItem[8];
            for (int i = 0; i < store_checks.Length; i++)
            {
                ToolStripMenuItem inner_menuitem = new ToolStripMenuItem("Check #" + i.ToString());
                inner_menuitem.Checked = store_checks[i];
                inner_menuitem.CheckOnClick = true;
                inner_menuitem.ShortcutKeys = Keys.Control | (Keys)(48 + i); //Di = 48 + i
                inner_menuitem.ShowShortcutKeys = true;
                inner_menuitem.Click += new EventHandler(inner_menuitem_Click);
                inner_menuitem.Tag = i.ToString();
                inner_menuitems[i] = inner_menuitem;
            }
            ToolStripMenuItem outer_menu = new ToolStripMenuItem("Outer Menu", null, inner_menuitems);
            outer_menu.DropDown.Closing += new ToolStripDropDownClosingEventHandler(DropDown_Closing);
            ContextMenuStrip context_menu = new ContextMenuStrip();
            context_menu.Items.Add(outer_menu);
            context_menu.Show(this, this.PointToClient(Cursor.Position));
        }

        void DropDown_Closing(object sender, ToolStripDropDownClosingEventArgs e)
        {
            if (e.CloseReason == ToolStripDropDownCloseReason.ItemClicked)
            {
                e.Cancel = true;
                ((ToolStripDropDownMenu)sender).Invalidate();
            }
        }

        void inner_menuitem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem sender_menu = (ToolStripMenuItem)sender;
            int index = int.Parse(sender_menu.Tag.ToString());
            store_checks[index] = !store_checks[index];
        }

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.richTextBox1 = new System.Windows.Forms.RichTextBox();
            this.SuspendLayout();
            //
            // richTextBox1
            //
            this.richTextBox1.Location = new System.Drawing.Point(13, 13);
            this.richTextBox1.Name = "richTextBox1";
            this.richTextBox1.Size = new System.Drawing.Size(100, 96);
            this.richTextBox1.TabIndex = 0;
            this.richTextBox1.Text = "";
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.richTextBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
        }

        #endregion

        private System.Windows.Forms.RichTextBox richTextBox1;
    }
}
并在方法中使用以下代码(无论菜单有多深都有效):

inner_menuitem.Click += new EventHandler(inner_menuitem_Can_Close);
void inner_menuitem_Can_Close(object sender, EventArgs e)
{
    ToolStripMenuItem castSender = (ToolStripMenuItem)sender;
    object owner = castSender.OwnerItem;
    while (owner is ToolStripMenuItem)
    {
        if (((ToolStripMenuItem)owner).Owner is ContextMenuStrip)
            ((ContextMenuStrip)((ToolStripMenuItem)owner).Owner).Close();
        owner = ((ToolStripMenuItem)owner).OwnerItem;
    }
}