| How to create a Checked Context Menu Item? |
|
Question: How to create a Checked Context Menu Strip Menu Item?
Add items to Explorer Shell context menu easily – How it may be done ?
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 :
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.
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); } } } |
||