MenuBuilder ClassCleanCode C# Libraries v1.2.03 API
Provides convenience methods for building a context menu.
Inheritance Hierarchy

OnlineSystem Object
  CleanCode.Forms MenuBuilder

Namespace: CleanCode.Forms
Assembly: CleanCode (in CleanCode.dll) Version: (1.2.03)

public class MenuBuilder

The use of this class is more easily shown by example. The bulk of this example code comes from the ChameleonRichTextBoxControls.ChameleonRichTextBox code. I have also published an article that provides an in-depth discussion of how to use this class -- OnlineUsing LINQ to Manage File Resources and Context Menus -- available on the DevX online magazine.

The SetupContextMenu method should be called during your application initialization. This creates a context menu and attaches both menu items and submenus to it. The contextMenuStrip_Opening method initializes the menu when the user indicates to open it. By using constants in both methods it allows direct access to individual menu items (e.g. MENUITEM_ENABLE_HIGHLIGHT). The checked state of that menu item may then be set with a simple assignment of the current property value to the menu item accessed via GetMenuItem(String). For situations where a property value determines one of several menu choices to be checked, the GetMenuItem(String, String) accessor is available instead.
MenuBuilder menuBuilder;

private void SetupContextMenu()
   menuBuilder = new MenuBuilder(this);
   ContextMenuStrip = new ContextMenuStrip();
   ContextMenuStrip.Opening += new CancelEventHandler(contextMenuStrip_Opening);

   // Menu item on top level context menu
   menuBuilder.CreateMenuItem(ContextMenuStrip, MENUITEM_ENABLE_FOOBAR,
       enableHighlightMenuItem_Click, "EnableFoobar");

   // Submenu on top level context menu
   ToolStripMenuItem highlightMenu = menuBuilder.CreateSubMenu(ContextMenuStrip, SUBMENU_HIGHLIGHT);
   menuBuilder.CreateMenuItem(highlightMenu, MENUITEM_ENABLE_HIGHLIGHT, 
       enableHighlightMenuItem_Click, "EnableHighlighting");
   menuBuilder.CreateMenuItem(highlightMenu, MENUITEM_HIDE_HIGHLIGHT_ON_DISABLE, 
       hideHighlightWhenDisabledMenuItem_Click, "HideHighlightingWhenDisabled");

   // Submenu on highlight submenu
   ToolStripMenuItem keywordMenu = menuBuilder.CreateSubMenu(highlightMenu, SUBMENU_KEYWORDS);
   menuBuilder.CreateMenuItem(keywordMenu, MENUITEM_DEFAULT_CASE,
       defaultCaseKeywordsMenuItem_Click, "HighlightKeywordAction");
   menuBuilder.CreateMenuItem(keywordMenu, MENUITEM_UPPERCASE,
       uppercaseKeywordsMenuItem_Click, "HighlightKeywordAction");
   menuBuilder.CreateMenuItem(keywordMenu, MENUITEM_LOWERCASE,
       lowercaseKeywordsMenuItem_Click, "HighlightKeywordAction");
   . . .

 private void contextMenuStrip_Opening(object sender, CancelEventArgs e)
   // start with a clean slate 
   foreach (ToolStripMenuItem item in ContextMenuStrip.Items.All())
   { item.Checked = false; }

   // set the direct menu item states
   menuBuilder.GetMenuItem(MENUITEM_ENABLE_HIGHLIGHT).Checked = EnableHighlighting;
   menuBuilder.GetMenuItem(MENUITEM_ENABLE_FOOBAR).Checked = EnableFoobar;

   // set the inferred menu item states (like a radio button) 
   string choice;
   switch (HighlightKeywordAction)
       case HighlightOption.ToLowerCase: choice = MENUITEM_LOWERCASE; break;
       case HighlightOption.ToUpperCase: choice = MENUITEM_UPPERCASE; break;
       default: choice = MENUITEM_DEFAULT_CASE; break;
   menuBuilder.SetMenuItemCheckmarkTrue(SUBMENU_KEYWORDS, choice);
   . . .

Since CleanCode 0.9.26.

See Also