DekGenius.com
[ Team LiB ] Previous Section Next Section

MenuItemmarshal by reference, disposable

System.Windows.Forms (system.windows.forms.dll)class

This Menu class represents the submenus of either a MainMenu, ContextMenu, or other MenuItem object. You can add it to the MenuItems property of any of these classes to create such a submenu.

You can control the appearance of the item with a number of different properties. You can set the Text for the menu item, and it can be Enabled, and made Visible. The BarBreak and Break properties cause the menu item to start a new column in the menu, with and without a dividing line, respectively. Checked determines whether a check mark appears next to item text, and RadioCheck determines whether the check mark appears as a radio button instead of a checkmark. Note that you have to handle the mutual exclusion yourself. You can set a Shortcut key to associate with the item, and ShowShortcut determines whether the key is displayed to the right of the text. You can also find out which Mnemonic character is acting as an Alt key shortcut.

To support MDI applications, you can mark an item as the MdiListItem. The system will automatically create and maintain a child menu for this object with a list of the MDI windows owned by the parent Form.

Also for the benefit of MDI and similar applications, Menu objects allow the merging of their items to meld together menus from disparate sources. To control this, you can specify the MergeOrder of a particular item. This is essentially a numeric priority for the item when merging two submenus with the same name. A low number will appear higher in the menu than a higher number. It is best to think of these as numeric groups where the same numbers will be bundled together in the final menu.

In addition to the MergeOrder, you can specify the MergeType. This allows you to specify whether the system will Add() the item to the existing collection (this is the default), Remove() the item when merging (i.e., it will not appear in the final menu), Replace any existing items at the same position in the merged menu, or MergeItems: interleave the items with the existing items, adding and replacing as appropriate. Note that the settings on both parties in the merge affect the end results. It is best to play with these options until you get a feel for how menu merging works in practice.

To determine when items are being manipulated, you can bind to the Click, Popup (a child menu is about to appear), and Select (the item has been highlighted) events. You can programmatically raise these events with the PerformClick() and PerformSelect() methods.

Note that MenuItem objects also support OwnerDraw. See MeasureItemEventArgs and DrawItemEventArgs for more information on owner draw facilities.

public class MenuItem : Menu {
// Public Constructors
   public MenuItem();
   public MenuItem(MenuMerge mergeType, int mergeOrder, Shortcut shortcut, string text, EventHandler onClick, 
        EventHandler onPopup, EventHandler onSelect, MenuItem[ ] items);
   public MenuItem(string text);
   public MenuItem(string text, EventHandler onClick);
   public MenuItem(string text, EventHandler onClick, Shortcut shortcut);
   public MenuItem(string text, MenuItem[ ] items);
// Public Instance Properties
   public bool BarBreak{set; get; }
   public bool Break{set; get; }
   public bool Checked{set; get; }
   public bool DefaultItem{set; get; }
   public bool Enabled{set; get; }
   public int Index{set; get; }
   public override bool IsParent{get; }
// overrides Menu
   public bool MdiList{set; get; }
   public int MergeOrder{set; get; }
   public MenuMerge MergeType{set; get; }
   public char Mnemonic{get; }
   public bool OwnerDraw{set; get; }
   public Menu Parent{get; }
   public bool RadioCheck{set; get; }
   public Shortcut Shortcut{set; get; }
   public bool ShowShortcut{set; get; }
   public string Text{set; get; }
   public bool Visible{set; get; }
// Protected Instance Properties
   protected int MenuID{get; }
// Public Instance Methods
   public virtual MenuItem CloneMenu();
   public virtual MenuItem MergeMenu();
   public void MergeMenu(MenuItem itemSrc);
   public void PerformClick();
   public virtual void PerformSelect();
   public override string ToString();  // overrides Menu
// Protected Instance Methods
   protected void CloneMenu(MenuItem itemSrc);
   protected override void Dispose(bool disposing);  // overrides Menu
   protected virtual void OnClick(EventArgs e);
   protected virtual void OnDrawItem(DrawItemEventArgs e);
   protected virtual void OnInitMenuPopup(EventArgs e);
   protected virtual void OnMeasureItem(MeasureItemEventArgs e);
   protected virtual void OnPopup(EventArgs e);
   protected virtual void OnSelect(EventArgs e);
// Events
   public event EventHandler Click;
   public event DrawItemEventHandler DrawItem;
   public event MeasureItemEventHandler MeasureItem;
   public event EventHandler Popup;
   public event EventHandler Select;
}

Hierarchy

System.Object System.MarshalByRefObject System.ComponentModel.Component(System.ComponentModel.IComponen, System.IDisposable) Menu MenuItem

Returned By

Menu.{FindMenuItem(), MdiListItem}, MenuItemCollection.this

Passed To

ContextMenu.ContextMenu(), System.Windows.Forms.Design.IMenuEditorService.SetSelection(), MainMenu.MainMenu(), Menu.Menu(), MenuItemCollection.{Add(), AddRange(), Contains(), IndexOf(), Remove()}, MenuItem.MenuItem()

    [ Team LiB ] Previous Section Next Section