Magic Logo
top
notifications
properties
events
sample code
Class MenuControl
The usual way to create a menu for your application is via the Menu property of your applications main Form. Unfortunately this gives a very basic look and feel and does not support the new Visual Studio .NET appearance.

This is where the MenuControl class comes into its own as it closely mimics the Visual Studio .NET look and feel. Not only that, but you can create as many instances as you like and place them anywhere on your Form. You can even use the Dock property to place the control against the left/right and bottom edges as well as the traditional top position. When placed on the left or right edges it will draw the menu items vertically.

When using the control within an MDI application it is important that exactly one instance of the MenuControl uses the MdiContainer property. This property should be given a reference to the MDI parent Form so that it can correctly display the pendant for MDI child Forms. See the SampleMenus project that comes with the library for an example of how to do this.

Notifications
When constructing a hierarchy of MenuCommand objects for your application menu you will want to attach to the Click and Update events. These provide the core mechanism for your application to react to menu selections. For a more detailed description and example code on using these events read the MenuCommand Class article.

If you need to dynamically change the contents of the PopupMenu that is created when a top-level menu is selected you should hook up to the PopupStart event. The order of events is such that the PopupStart is fired first and then each MenuCommand for the PopupMenu has its Update event fired. Once the PopupMenu has been dismissed, for whatever reason, the PopupEnd event is fired.

You could use these events to dynamically generate the list of appropriate MenuCommand instances before the popup menu is shown. However, you need to be careful, as the MenuControl will only realise that a PopupMenu is required for a top-level menu item if there is at least one MenuCommand associated with it. So you would need to add a single placeholder MenuCommand in order to cause the control to generate a PopupMenu which could then be dynamically filled as required. On receiving the PopupEnd event the contents could be removed and the placeholder inserted again.

Most windows applications will update the status bar with a menu description as the user selects different menu items. To help support this scenario the Selected and Deselected events are provided. As the names suggest, the Selected event is generated as the selection enters a menu item and the Deselected event when it moves away.

Properties
public MenuCommandCollection MenuCommands
Collection of MenuCommand instances that represent a submenu.

public VisualStyle Style
Has two possible values

  • VisualStyle.IDE shows in Visual Studio .NET style
  • VisualStyle.Plain shows in a simpler Visual C++ v6 style
public Direction Direction
Has two possible values
  • Direction.Vertical draw items down the control height
  • Direction.Horizontal draw items across the control width
Default: Dependant on value of the Dock property.

public override DockStyle Dock
Determines which, if any, edge the control is docked against.
Default: DockStyle.Top

public override Font Font
Specifies the Font to use when drawing menu text
Default: SystemInformation.MenuFont

public override Color BackColor
Specifies the Color to use when drawing the Control background.
Default: SystemColors.Control

public Color TextColor
Text drawing color when not selected or highlighted.
Default: SystemColors.MenuText

public Color HighlightBackColor
Base color for drawing color inside highlighted menu item.
Default: SystemColors.Highlight

public Color HighlightTextColor
Text drawing color when not selected but is highlighted.
Default: SystemColors.MenuText

public Color SelectedBackColor
Base color for drawing color inside selected menu item.
Default: SystemColors.Control

public Color SelectedTextColor
Text drawing color when selected.
Default: SystemColors.MenuText

public Color PlainSelectedTextColor
When VisualStyle.Plain style is selected this color is used for drawing selected text.
Default: SystemColors.ActiveCaptionText

public bool MultiLine
When defined this will create extra rows/columns to fit in all the required menu items, when not defined it will show a chevron to allow invisible items to be selected.
Default: false

public bool PlainAsBlock
This alters the appearance of selected menu items when VisualStyle.Plain is defined.
Default: false

public Form MdiContainer
In an MDI application this should be set to the Form that acts as the parent to ensure pendant operates correctly.
Default: null

public bool RememberExpansion
When an expansion button is used on a popup menu it will remember this and auto expand it when next shown.
Default: true

public bool ExpandAllTogether
If any popup menu is expanded then all other popup menus for this control will automatically be shown expanded.
Default: true

public bool DeselectReset
When the control is deselected all the popup menus are reset to not showing expanded when next displayed.
Default: true

public bool HighlightInfrequent
Items marked as Infrequent are drawn in a different visual style.
Default: true

public Animate Animate
Can be set to one of the following enumeration values

  • Animate.No do not animate popup menus
  • Animate.Yes always animate popup menus
  • Animate.System use system setting
Default: Animate.System property.
public int AnimateTime
Determines the total time the animation should take in milliseconds.
Default: 100

public Animation AnimateStyle
Can be set to one of the following enumeration values

  • Animation.Blend
  • Animation.SlideCenter
  • Animation.SlideHorVerPositive
  • Animation.SlideHorVerNegative
  • Animation.SlideHorPosVerNegative
  • Animation.SlideHorNegVerPositive
  • Animation.System
Default: Animation.System property.

Events
public event CommandHandler Selected
Fired when the user moves the selection over this item.
Use this event to update any status bar description appropriate for the item.

public event CommandHandler Deselected
Fired when the user selection moves away from this item.
Use this event to remove any status bar description previously set.

Delegate Signatures


    void CommandHandler(MenuCommand item);

Sample code
C#

   protected void AddMenu()
   {
      // Create the MenuControl instance
      MenuControl topMenu = new MenuControl();
		
      // Create and add a single top level menu item
      MenuCommand top = new MenuCommand("TestTop");

      // Associate appropriate status bar description for item
      top.Description = "Description of the submenu";

      topMenu.MenuCommands.Add(top);

      // Create and add a submenu with two items
      MenuCommand sub1 = new MenuCommand("TestSub1");
      MenuCommand sub2 = new MenuCommand("TestSub2");

      // Associate appropriate status bar descriptions for each menu
      sub1.Description = "Description for TestSub1";
      sub2.Description = "Description for TestSub2";

      top.MenuCommands.AddRange(new MenuCommand[]{sub1, sub2});

      // Hook into selection events
      _topMenu.Selected += new CommandHandler(OnSelected);
      _topMenu.Deselected += new CommandHandler(OnDeselected);

      // Add to the Form
      Controls.Add(_topMenu);
   }

   protected void OnSelected(MenuCommand mc)
   {
      _statusBarPanel.SetStatusBarText(mc.Description);
   }

   protected void OnDeselected(MenuCommand mc)
   {
      _statusBarPanel.SetStatusBarText("");
   }
VB.NET

   Protected Sub AddMenu()
      ' Create the MenuControl instance
      Dim topMenu As New MenuControl()
		
      ' Create and add a single top level menu item
      Dim top As New MenuCommand("TestTop")

      ' Associate appropriate status bar description for item
      top.Description = "Description of the submenu"

      topMenu.MenuCommands.Add(top)

      ' Create and add a submenu with two items
      Dim sub1 As New MenuCommand("TestSub1")
      Dim sub2 As New MenuCommand("TestSub2")

      ' Associate appropriate status bar descriptions for each menu
      sub1.Description = "Description for TestSub1"
      sub2.Description = "Description for TestSub2"

      top.MenuCommands.AddRange(new MenuCommand(){sub1, sub2})

      ' Hook into selection events
      AddHandler _topMenu.Selected, AddressOf OnSelected
      AddHandler _topMenu.Deselected, AddressOf OnDeselected

      ' Add to the Form
      Controls.Add(_topMenu)
   End Sub

   Protected Sub OnSelected(ByVal mc As MenuCommand)
      _statusBarPanel.SetStatusBarText(mc.Description)
   End Sub

   Protected Sub OnDeselected(ByVal mc As MenuCommand)
      _statusBarPanel.SetStatusBarText("")
   End Sub