I want to create a context menu that has several columns. Basically it would go like this:
First item | [common option] | All Options >
Second item | [common option] | All Options >
Third item | [common option] | All Options >
Fourth item | [common option] | All Options >
So basically there are a bunch of items (generated at runtime), each item can be launched on its own; or with a commonly used option; or you can get a submenu with all possible options.
How can I do this? I'm trying to abuse both ContextMenuStrip
and ContextMenu
, yet they don't seem to have any such options. Still I seem to recall having seen multi-column menus somewhere...
I'd prefer a Windows Forms solution, because I don't have any WPF experience. Oh, and this context menu will open when clicking on an icon in the Notification Area (aka systray).
In Windows Forms, a context menu is created using the ContextMenuStrip control and its command or menu items are ToolStripMenuItem objects. The Image property of ToolStripMenuItem is used to add an image to a menu item.
C# context menus are the menus that pop up when the user clicks with the right hand mouse button over a control or area in a Windows based form. They are called context menus because the menu is usually specific to the object over which the mouse was clicked.
In a Windows environment, the context menu is accessed with a right mouse click. For example, if the end user right-clicks in a Word document, the pop-up menu will include shortcuts for undo, cut, copy and paste.
The ContextMenuStrip control represents a shortcut menu that pops up over controls, usually when you right click them. They appear in context of some specific controls, so are called context menus. For example, Cut, Copy or Paste options.
I don't know about ContextMenuStrip
, which is a menu built entirely in .NET code, but you can definitely do this with ContextMenu
, which is a wrapper over the native system menus.
The key is setting the MFT_MENUBREAK
or MFT_MENUBARBREAK
flags for the individual menu item(s), which are exposed as properties in the MenuItem
class wrapper: MenuItem.Break
and MenuItem.BarBreak
, respectively.
The former just places the menu item in a new column, while the latter places the item into a new column and separates the column with an etched vertical line.
From the MSDN example:
public void CreateMyMenus()
{
// Create three top-level menu items.
MenuItem menuItem1 = new MenuItem("&File");
MenuItem menuItem2 = new MenuItem("&New");
MenuItem menuItem3 = new MenuItem("&Open");
// Set the BarBreak property to display horizontally.
menuItem2.BarBreak = true;
menuItem3.BarBreak = true;
// Add menuItem2 and menuItem3 to the menuItem1's list of menu items.
menuItem1.MenuItems.Add(menuItem2);
menuItem1.MenuItems.Add(menuItem3);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With