Visual Basic Concepts
Creating and Modifying Menus at Run Time
The menus you create at design time can also respond dynamically to run-time conditions. For example, if a menu item action becomes inappropriate at some point, you can prevent users from selecting that menu item by disabling it. In the MDI NotePad application, for example, if the clipboard doesn't contain any text, the Paste menu item is dimmed on the Edit menu, and users cannot select it.
You can also dynamically add menu items, if you have a menu control array. This is described in "Adding Menu Controls at Run Time," later in this topic.
You can also program your application to use a check mark to indicate which of several commands was last selected. For example, the Options, Toolbar menu item from the MDI NotePad application displays a check mark if the toolbar is displayed. Other menu control features described in this section include code that makes a menu item visible or invisible and that adds or deletes menu items.
Enabling and Disabling Menu Commands
All menu controls have an Enabled property, and when this property is set to False, the menu is disabled and does not respond to user actions. Shortcut key access is also disabled when Enabled is set to False. A disabled menu control appears dimmed, like the Paste menu item in Figure 6.11.
Figure 6.11 A disabled menu item
For example, this statement disables the Paste menu item on the Edit menu of the MDI NotePad application:
mnuEditPaste.Enabled = False
Disabling a menu title in effect disables the entire menu, because the user cannot access any menu item without first clicking the menu title. For example, the following code would disable the Edit menu of the MDI Notepad application:
mnuEdit.Enabled = False
Displaying a Check Mark on a Menu Control
Using the Checked property, you can place a check mark on a menu to:
Tell the user the status of an on/off condition. Choosing the menu command alternately adds and removes the check mark.
Indicate which of several modes is in effect. The Options menu of the MDI Notepad application uses a check mark to indicate the state of the toolbar, as shown in Figure 6.12.
Figure 6.12 A checked menu item
You create check marks in Visual Basic with the Checked property. Set the initial value of the Checked property in the Menu Editor by selecting the check box labeled Checked. To add or remove a check mark from a menu control at run time, set its Checked property from code. For example:
Private Sub mnuOptions_Click ()
' Set the state of the check mark based on
' the Visible property.
mnuOptionsToolbar.Checked = picToolbar.Visible
End Sub
Making Menu Controls Invisible
In the Menu Editor, you set the initial value of the Visible property for a menu control by selecting the check box labeled Visible. To make a menu control visible or invisible at run time, set its Visible property from code. For example:
mnuFileArray(0).Visible = True ' Make the control
' visible.
mnuFileArray(0).Visible = False ' Make the control
' invisible.
When a menu control is invisible, the rest of the controls in the menu move up to fill the empty space. If the control is on the menu bar, the rest of the controls on the menu bar move left to fill the space.
**Note **Making a menu control invisible effectively disables it, because the control is inaccessible from the menu, access or shortcut keys. If the menu title is invisible, all the controls on that menu are unavailable.
Adding Menu Controls at Run Time
A menu can grow at run time. In Figure 6.13, for example, as files are opened in the SDI NotePad application, menu items are dynamically created to display the path names of the most recently opened files.
Figure 6.13 Menu control array elements created and displayed at run time
You must use a control array to create a control at run time. Because the mnuRecentFile menu control is assigned a value for the Index property at design time, it automatically becomes an element of a control array — even though no other elements have yet been created.
When you create mnuRecentFile(0), you actually create a separator bar that is invisible at run time. The first time a user saves a file at run time, the separator bar becomes visible, and the first file name is added to the menu. Each time you save a file at run time, additional menu controls are loaded into the array, making the menu grow.
Controls created at run time can be hidden by using the Hide method or by setting the control's Visible property to False. If you want to remove a control in a control array from memory, use the Unload statement.