Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 983201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:49:08+00:00 2026-05-16T04:49:08+00:00

I have been unable to figure out how to keep a context menu open

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:49:08+00:00Added an answer on May 16, 2026 at 4:49 am

    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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been unable to trigger an onselect event handler attached to a <div>
I have been unable to receive UDP multicast under VxWorks 5.5. I've joined the
Have been looking at the MVC storefront and see that IQueryable is returned from
Have been studying the file system related classes of Adobe AIR 1.5, but so
We have been using CruiseControl for quite a while with NUnit and NAnt. For
I have been experimenting with woopra.com A web analytics tool. Which requires a piece
I have been working on a web services related project for about the last
I have been working with Visual Studio (WinForm and ASP.NET applications using mostly C#)
I have been looking into IKVMing Apache's FOP project to use with our .NET
I have been searching everywhere for the following functionality in Lisp, and have gotten

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.