How to create a Checked Context Menu Item?

Question: 

How to create a Checked Context Menu Strip Menu Item?
Zones: Visual Studio .NET 2005, C# Programming Language
Tags: C# Win Forms


I want to create a context menu and want to allow the user to select multiple items in the context menu show. How can I do that for Win Forms, C#, .Net 2.0 ?

Add items to Explorer Shell context menu easily – How it may be done ?

 

 

 Add items to Explorer Shell context menu with Windows Explorer Shell Context Menu

 

  Add an item to Windows Explorer Shell context menu easily with Windows Explorer Shell Context Menu. This powerful .Net component for your own, custom items appending to Windows Explorer Shell context menu will add all your application entries to Explorer Shell context menu. This .Net component and VB.NET support include detailed C# / VB.NET samples, tutorials , user-friendly manuals and support all you may need to add your items to context menu :

  • Add all your items to Windows Explorer Shell context menu to be shown on any Windows OS (all OS are supported – XP, Vista, x64 of all types , etc.)
  • Add any type of items to Windows Explorer Shell context menu to be shown in any way - with custom caption and icon, as separator or sub-menu
  • Add items of any types to Windows Explorer Shell context menu to be shown for all types of files or shown only for files of particular type (for example, only for .PDF .TXT , .MP3,.WMA,.AAC , .MPG files)
  • Add items to Windows Explorer Shell context menu, sub-menus, sub-menus of unlimited depth and even more


Windows Explorer Shell Context Menu - is a .Net framework component that support all you need to insert all your program items to Explorer Shell context menu - in a fast and easy way. Add your application items to Explorer context menu right now – add items to context menu fast , easy and exactly as you prefere :

 Insert items To Explorer Shell Context Menus easily with Windows Explorer Shell Context Menu

 

Answer:

Information in this answer for creating checked context menu items in your applications may be used only for several old Windows operating systems, because this checked menu item appending to context menu method works only for Windows 95 / Windows 98 (not on XP, Vista, x64 - 64-bit Windows), to add items to Windows Explorer Shell context menu you should use, according to Microsoft guidelines, appropriate .Net component - Windows Explorer Shell Context Menu. When clicking on a menu item, set this menuitem property Checked to Not Checked. (which will toggle from unchecked to checked to unchecked, etc)

If you want that the context menu is kept open than reshow it just after changing the checked state.

  1. Create a new form
  2. Add a ContextmenuStrip to it
  3. Paste the attached code:

using System;
using System.ComponentModel;
using System.Windows.Forms;
 
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitializeContextMenu();
        }
 
        private void InitializeContextMenu()
        {
            ToolStripMenuItem contextMenuItem;
 
            contextMenuItem = new ToolStripMenuItem("Context Menu Item 1");
            contextMenuItem.Click += new System.EventHandler(this.contextMenuItem_Click);
            this.contextMenuStrip1.Items.Add(contextMenuItem);
 
            contextMenuItem = new ToolStripMenuItem("Context Menu Item 2");
            contextMenuItem.Click += new System.EventHandler(this.contextMenuItem_Click);
            this.contextMenuStrip1.Items.Add(contextMenuItem);
 
            contextMenuItem = new ToolStripMenuItem("Context Menu Item 3");
            contextMenuItem.Click += new System.EventHandler(this.contextMenuItem_Click);
            this.contextMenuStrip1.Items.Add(contextMenuItem);
 
            this.contextMenuStrip1.Closing += new ToolStripDropDownClosingEventHandler(this.contextMenuStrip1_Closing);
            this.contextMenuStrip1.Closed += new ToolStripDropDownClosedEventHandler(this.contextMenuStrip1_Closed);
 
            this.ContextMenuStrip = this.contextMenuStrip1;
        }
 
        private void contextMenuItem_Click(object sender, EventArgs e)
        {
            ((ToolStripMenuItem)sender).Checked ^= true;
        }
 
        private void contextMenuStrip1_Closing(object sender, ToolStripDropDownClosingEventArgs e)
        {
            if (e.CloseReason == ToolStripDropDownCloseReason.ItemClicked)
            {
                e.Cancel = true;
            }
        }
 
        private void contextMenuStrip1_Closed(object sender, ToolStripDropDownClosedEventArgs e)
        {
            String checkedItems = "";
            foreach (ToolStripMenuItem menuItem in this.ContextMenuStrip.Items)
            {
                if (menuItem.Checked) checkedItems += menuItem.Text + "\n";
            }
            MessageBox.Show(checkedItems);
        }
 
    }
}