Page 217 - Open Soource Technologies 304.indd
P. 217
Unit 9: Menus and Dialog Boxes
9.5.4 Adding Menu Controls at Run Time
A menu can grow at run time. In Figure 9.8, 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 9.8: Menu Control array Elements Created and Displayed at Run Time.
We 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 we
create mnuRecentFile(0), we 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 we want to remove a control
in a control array from memory, we will use the Unload statement.
9.6 Modal and Modeless Dialog Boxes
Dialog boxes are either modal or modeless. A modal dialog box must be closed (hidden or
unloaded) before you can continue working with the rest of the application. For example, a
dialog box is modal if it requires you to click OK or Cancel before you can switch to another
form or dialog box.
The About dialog box in Visual Basic is modal. Dialog boxes that display important messages
should always be modal — that is, the user should always be required to close the dialog box
or respond to its message before proceeding.
Modeless dialog boxes let you shift the focus between the dialog box and another form without
having to close the dialog box. You can continue to work elsewhere in the current application
while the dialog box is displayed. Modeless dialog boxes are rare. From the Edit menu, the Find
dialog box in Visual Basic is an example of a modeless dialog box. Use modeless dialog boxes
to display frequently used commands or information.
LOVELY PROFESSIONAL UNIVERSITY 211