Magic Logo
top
constructors
notifications
methods
properties
events
sample code
Class MenuCommand
Each MenuCommand instance represents a single item that can appear in either a PopupMenu or MenuControl instance.
Constructors

   public MenuCommand()
   public MenuCommand(string text)
   public MenuCommand(string text, EventHandler clickHandler)
   public MenuCommand(string text, Shortcut shortcut)

   public MenuCommand(string text, Shortcut shortcut, 
                      EventHandler clickHandler)

   public MenuCommand(string text, ImageList imageList, 
                     int imageIndex)

   public MenuCommand(string text, ImageList imageList, 
                     int imageIndex, Shortcut shortcut)

   public MenuCommand(string text, ImageList imageList, 
                      int imageIndex, EventHandler clickHandler)

   public MenuCommand(string text, ImageList imageList, 
                      int imageIndex, Shortcut shortcut, 
                      EventHandler clickHandler)

Notifications
There is little point in having a menu item unless you are notified when that item has been selected by the user. You can create and attach a standard EventHandler to the exposed MenuCommand.Click event in order to get notified when the command has been selected. Note that this can happen from either mouse or keyboard actions.

Another useful event is the MenuCommand.Update notification that is fired for each MenuCommand instance for a PopupMenu when it is about to be displayed. This allows you to update the correct state of the item only when it is relevant to do so. Note that only a PopupMenu will generate this event as a MenuControl instant is constantly visible and so any update to its commands will need to be made manually.


   MenuCommand item = new MenuCommand("TestMenuItem");

   // This is how to attach to relevant events
   item.Click += new EventHandler(OnItemClick);
   item.Update += new EventHandler(OnItemUpdate);

   ...

   protected void OnItemClick(object sender, EventArgs e)
   {
      Console.WriteLine("Menu item selected!");
   }

   protected void OnItemUpdate(object sender, EventArgs e)
   {
      // Must cast from generic object reference to our MenuCommand
      MenuCommand mc = sender as MenuCommand;
      
      // Update state, in this case invert the checked indicator
      mc.Check = !mc.Checked;
   }

Methods
public void PerformClick()
Updates the item state and generates a Click event.
Properties
public string Text
Text displayed in the menu item cell.
Default: "MenuItem"

public int ImageIndex
Index of the image to be shown in the menu item cell.
Default: -1

public ImageList ImageList
Images the MenuCommand.ImageIndex indexes.
Default: null

public Image Image
This image is used in preference to the ImageList/ImageIndex when defined.
Default: null

public Shortcut Shortcut
Keyboard combination that will fire the Click event.
Default: Shortcut.None

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

public bool Enabled
Defines if item can be selected.
Default: true

public bool Checked
Should a checkmark be displayed for this menu item.
Default: false

public bool RadioCheck
Alternative checkmark is drawn when this and MenuCommand.Checked are defined.
Default: false

public bool Break
Start new column before showing this item.
Default: false

public bool Infrequent
Only show this item when expansion mode is set.
Default: false

public bool Visible
Display this item in the menu.
Default: true

public bool IsParent
Indicates if this menu has a submenu.

public string Description
Description that is appropriate for display in a status bar.
Default: empty string

public object Tag
Arbitrary data associated with the object by developer.
Default: null

Events
public event CommandHandler PopupStart
Fired just before a PopupMenu is shown for this collection of commands.

public event CommandHandler PopupEnd
Fired when the PopupMenu is dimissed for this colleciton of commands.

public event EventHandler Click
Fired when this MenuCommand has been selected.

public event EventHandler Update
Fired before the menu item is display in a PopupMenu.

public event PropChangeHandler PropertyChanged
Used by the MenuControl to monitor changes in properties.

Delegate Signatures

                        
   void PropChangeHandler(MenuCommand item, Property prop);
        
Sample code
C#

   // Create the top-level PopupMenu commands
   MenuCommand country1 = new MenuCommand("&Sweden");
   MenuCommand country2 = new MenuCommand("S&pain");
   MenuCommand country3 = new MenuCommand("&Canada");

   // Disable Sweden
   country1.Enabled = false;

   // Place check mark against Canada
   country1.Checked = true;

   // Create the list of Spanish cities (with shortcut key combinations)
   MenuCommand spain1 = new MenuCommand("&Nerja", Shortcut.ShiftF1);
   MenuCommand spain2 = new MenuCommand("&Madrid", Shortcut.Alt0);
   MenuCommand spain3 = new MenuCommand("&Barcelona", Shortcut.ShiftIns);

   // Hook up some event handlers
   spain1.Click += new EventHandler(OnNerjaSelected);
   spain2.Click += new EventHandler(OnMadridSelected);
   spain3.Click += new EventHandler(OnBarcelonaSelected);

   // Add the cities submenu to the 'Spain' main command
   country2.MenuCommands.AddRange(new MenuCommand[]{spain1, spain2, spain3});

   PopupMenu popup = new PopupMenu();

   // Add the countries to the PopupMenu object
   popup.MenuCommands.AddRange(new MenuCommand[]{country1, country2, country3});

   // Show menu!
   MenuCommand selection = popup.TrackPopup(new Point(100,100));
   
VB.NET

   ' Create the top-level PopupMenu commands
   Dim country1 As New MenuCommand("&Sweden")
   Dim country2 As New MenuCommand("S&pain")
   Dim country3 As New MenuCommand("&Canada")

   ' Disable Sweden
   country1.Enabled = False

   ' Place check mark against Canada
   country1.Checked = True

   ' Create the list of Spanish cities (with shortcut key combinations)
   Dim spain1 As New MenuCommand("&Nerja", Shortcut.ShiftF1)
   Dim spain2 As New MenuCommand("&Madrid", Shortcut.Alt0)
   Dim spain3 As New MenuCommand("&Barcelona", Shortcut.ShiftIns)

   ' Hook up some event handlers
   AddHandler spain1.Click, AddressOf OnNerjaSelected
   AddHandler spain2.Click, AddressOf OnMadridSelected
   AddHandler spain3.Click, AddressOf OnBarcelonaSelected

   ' Add the cities submenu to the 'Spain' main command
   country2.MenuCommands.AddRange(new MenuCommand(){spain1, spain2, spain3})

   Dim popup As New PopupMenu()

   ' Add the countries to the PopupMenu object
   popup.MenuCommands.AddRange(new MenuCommand(){country1, country2, country3})

   ' Show menu!
   Dim selection As MenuCommand = popup.TrackPopup(New Point(100,100))