215
As with the OpenFileDialog component, most applications should set the InitialDirectory, Filter, and FilterIndex properties prior to calling ShowDialog. Their usage with the SaveFileDialog component is
precisely the same as with OpenFileDialog.
5.5 Menus
The Windows Forms library provides three components for creating and managing menus: •
The MainMenu class manages the display of a menu across the top of a form. •
The ContextMenu class manages the display of a context menu also known as a pop-up menu.
• The MenuItem class represents a specific menu item within a main menu or context menu.
Menus can be created either in the Visual Studio .NET Windows Forms Designer or programmatically.
5.5.1 Adding Menus in the Visual Studio .NET Windows Forms Designer
Visual Studio .NET includes an in-place WYSIWYG menu editor that is a dramatic improvement over the menu editor in Visual Basic 6. To create an application menu in the Windows Forms Designer, add
a MainMenu component to the form. This causes a representation of the component to appear in the lower pane of the forms design view. When the component is selected, Visual Studio .NET adds a
WYSIWYG editor to the top of the form, as shown in
Figur e 5- 22 .
Figure 5-22. In-place editing of a main menu
216
To create a menu item, type a value into the Type Here box. The menu editor automatically adds Type Here boxes to the right and beneath the box in which you are typing. As was the case in
previous Windows development environments, typing an ampersand within the menu name causes the character following the ampersand to be an accelerator key. Typing a single hyphen -
within a Type Here box creates a menu-separator bar.
Figur e 5- 23 shows a menu containing
several items.
Figure 5-23. A menu containing several items
After a menu item has been added, its properties can be set in the Properties window. The Properties window of the Edit
Paste item shown in Figur e 5- 23
is shown in Figur e 5- 24
.
Figure 5-24. The Properties window display for the Edit Paste menu
item shown in Figu r e 5 - 2 3
217
The properties shown in Figur e 5- 24
are: Checked
Indicates whether a checkmark appears next to the menu item. The type is Boolean. The default is
False .
DefaultItem Indicates whether the menu item is the default item in its menu or submenu. Default menu
items are displayed in bold. The type is Boolean. The default is False
. Enabled
Indicates whether the menu item is enabled. If this is False
, the menu item is grayed and cant be selected. The type is Boolean. The default is
True .
MdiList In MDI applications, indicates whether the menu item should have subitems for each of the
open MDI child forms. This property should be set to True
for the Windows menu item. The type is Boolean. The default is
False .
MergeOrder In applications where two menus might be merged such as an MDI application where a child
form menu might be merged with its parent form menu, sorts the merged menu items based on this property. The type is Integer. The default is
. See MDI Applications in Chapt er 4
for more information.
MergeType Indicates how two menu items having the same MergeOrder value should be merged. The
value is of type MenuMerge defined in the System.Windows.Forms namespace. The default is
MenuMerge.Add . See MDI Applications in
Chapt er 4 for more information.
218
Modifiers Specifies the declaration modifiers that are placed on the menu-item field declaration within
the generated code. This is not actually a property of the MenuItem class. Rather, it becomes part of the field declaration in source code. The default is
Private .
OwnerDraw Indicates whether the menu item requires custom drawing. If you set this property to
True ,
you must handle the menu items DrawItem event. The type is Boolean. The default is False
. RadioCheck
If the Checked property is True
, indicates whether to display the checkmark as a radio button instead. The type is Boolean. The default is
False .
Shortcut Specifies the shortcut key combination that invokes this menu item. The value is of type
Shortcut defined in the System.Windows.Forms namespace. The Shortcut enumeration defines a unique value for each potential shortcut key combination. The values of the Shortcut
enumeration are shown in Table 5- 1
. The default is Shortcut.None
.
Table 5-1. Values defined by the System.Windows.Forms.Shortcut enumeration
None Ins
Del F1
F2 F3
F4 F5
F6 F7
F8 F9
F10 F11
F12 ShiftIns
ShiftDel ShiftF1
ShiftF2 ShiftF3
ShiftF4 ShiftF5
ShiftF6 ShiftF7
ShiftF8 ShiftF9
ShiftF10 ShiftF11
ShiftF12 CtrlIns
CtrlDel CtrlA
CtrlB CtrlC
CtrlD CtrlE
CtrlF CtrlG
CtrlH CtrlI
CtrlJ CtrlK
CtrlL CtrlM
CtrlN CtrlO
CtrlP CtrlQ
CtrlR CtrlS
CtrlT CtrlU
CtrlV CtrlW
CtrlX CtrlY
CtrlZ CtrlF1
CtrlF2 CtrlF3
CtrlF4 CtrlF5
CtrlF6 CtrlF7
CtrlF8 CtrlF9
CtrlF10 CtrlF11
CtrlF12 CtrlShiftA
CtrlShiftB CtrlShiftC
CtrlShiftD CtrlShiftE
CtrlShiftF CtrlShiftG
CtrlShiftH CtrlShiftI
CtrlShiftJ CtrlShiftK
CtrlShiftL CtrlShiftM
CtrlShiftN CtrlShiftO
CtrlShiftP CtrlShiftQ
CtrlShiftR CtrlShiftS
CtrlShiftT CtrlShiftU
CtrlShiftV CtrlShiftW
CtrlShiftX CtrlShiftY
CtrlShiftZ CtrlShiftF1
CtrlShiftF2 CtrlShiftF3
CtrlShiftF4 CtrlShiftF5
219
CtrlShiftF6 CtrlShiftF7
CtrlShiftF8 CtrlShiftF9
CtrlShiftF10 CtrlShiftF11
CtrlShiftF12 AltBksp
AltF1 AltF2
AltF3 AltF4
AltF5 AltF6
AltF7 AltF8
AltF9 AltF10
AltF11 AltF12
ShowShortcut If a shortcut key combination is defined for the menu item, indicates whether the key
combination should be shown on the menu. The type is Boolean. The default is True
. Text
Represents the text shown on the menu item. The type is String. The default is an empty string.
Visible Indicates whether the menu item should be visible. The type is Boolean. The default is
True .
The Windows Forms Designer creates code that declares a field for the MainMenu object, as well as fields for the MenuItem objects that represent each menu item in the menu. The designer also creates
code that instantiates the objects at runtime and sets their properties according to the values set in the IDEs Properties window. In addition, the top-level MenuItem objects are added to the MenuItems
collection of the MainMenu object, and the lower-level MenuItem objects are added to the MenuItems collection of the menu of which they are submenus. Finally, the MainMenu object is assigned to the
forms Menu property.
To create a context menu in the Windows Forms Designer, add a ContextMenu component to the form. This causes a representation of the component to appear in the lower pane of the forms design view.
When the component is selected, Visual Studio .NET adds a WYSIWYG editor to the top of the form. Clicking on the editor drops down a Type Here box, as shown in
Figur e 5- 25 .
Figure 5-25. In-place editing of a context menu
Adding menu items and setting their properties work the same with context menus as with main menus. For a context menu to be displayed, it must be associated with some control on the form or
with the form itself. When the user right-clicks on that control or form, the context menu is displayed. To make this association, perform these steps:
1. In the Windows Forms Designer, right-click the control or form that is to be associated with the context menu, and choose Properties.
220
2. In the Properties window, find the ContextMenu property, and click the arrow of the drop-down list associated with it.
3. The drop-down list displays the ContextMenu objects that are defined on the current form. Choose the desired one.
The same effect can be achieved in code by assigning the ContextMenu object to the ContextMenu property of a control or form, like this:
Somewhere within the definition of a Form class. Me.Button1.ContextMenu = Me.ContextMenu1
5.5.2 Programmatically Creating Menus