MenuStrip


The MenuStrip control represents a form’s menus, submenus, and menu items. To make the form display the menu, its Menu property must be set to the MenuStrip control. The first time you add a MenuStrip control to the form, Visual Basic automatically sets the form’s Menu property to the new control, so you usually don’t need to worry about this. If you later delete the control and create a new MenuStrip, you may need to set this property yourself.

At design time, the MenuStrip control is visible both at the top of the form and in the component tray. Click a menu entry and type to change the caption it displays. Place an ampersand in front of the character that you want to use as a keyboard accelerator. For example, to make the caption of the File menu display as File, set the menu’s text to &File. If you type Alt+F at runtime, the program opens this menu.

To make a cascading submenu, click a menu item and enter its caption. A ghostly text box appears to the right containing the text “Type Here.” Click this text and enter the submenu’s name.

When you select a menu item, the Properties window displays the menu item’s properties. The following table describes the most useful menu item properties.

Open table as spreadsheet

Property

Purpose

Checked

Indicates whether the item is checked. You can use this property to let the user check and uncheck menu items.

CheckOnClick

Determines whether the item should automatically check and uncheck when the user clicks it.

CheckState

Determines whether the item is checked, unchecked, or displayed as in an indeterminate state.

DisplayStyle

Determines whether the item displays text, an image, both, or neither. The image appears on the left where a check box would otherwise go. If the item displays an image, it draws a box around the image when the item is checked.

Enabled

Determines whether the menu item is enabled. If an item is disabled, its shortcut is also disabled and the user cannot open its submenu if it contains one.

Font

Determines the font used to draw the item.

MergeAction

Determines how Visual Basic merges MDI child and parent form menus. See the online help for more information.

MergeIndex

Determines the order in which Visual Basic merges MDI child and parent form menus. See the online help for more information.

Name

Gives the menu item’s name.

ShortcutKeys

Determines the item’s keyboard shortcut. For instance, if you set an item’s Shortcut to F5, then the user can instantly invoke the item at runtime by pressing the F5 key.

ShowShortcutKeys

Determines whether the menu displays its shortcut to the right at runtime. Usually this should be True, so users can learn about the items’ shortcuts.

Text

Gives the caption displayed by the item. Place an ampersand in front of the character you want to use as a keyboard accelerator as already described.

Visible

Determines whether the item is visible. An item’s shortcut will still work even if the item is not visible. You can use that feature to provide keyboard shortcuts for functions that are not available in any menu.

When the user selects a menu item, the item raises a Click event. You can write an event handler to take whatever action is appropriate. For example, the following code shows how the File menu’s Exit item closes the form:

  Private Sub mnuFileExit_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles mnuFileExit.Click     Me.Close() End Sub 

To create a menu item event handler, open the item in the form editor and double-click it.

For online help about the MenuStrip control, go to msdn2.microsoft.com/en-us/library/ system.windows.forms.menustrip.aspx.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net