简体   繁体   中英

ContextMenu Will Not Stay Open C# .Net 4.0 Client Profile WinForms

I have been unable to figure out how to keep a context menu open after handling a click event past the first level. Here is an example of where I have a context menu with a menu of checkable menus. I open up the context menu after handling the click event, but I have to manually return to the inner menu. Is there a way to open up the outer menu programmatically or to prevent the inner menu from closing?

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;
        }
    }
}

Also, is there any way to get the "Control + number" shortcuts to work and activate the click event? Thanks in advance for the help!

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

I did not find any way to prevent the context menu from closing so instead I used ContextMenuStrip and ToolStripMenuItem. This also fixed the problem I had with shortcuts not working before. I handle the Closing event of the menu containing the checkable items and cancel closing if the items were clicked/checked.

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;
    }
}

You can also select certain buttons from within your unclosable submenu to cause the context menu to close normally. For a specific ToolStripMenuItem to close the menu normally, give it a different event method to call:

inner_menuitem.Click += new EventHandler(inner_menuitem_Can_Close);

And use the following code in the method (works regardless of how deep the menus go):

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM