Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Disabling (Graying Out) Menu Items

To indicate to the user that a menu item is not available at a particular time (such as Copy when there is no selected text), you can disable a menu item (also called “graying it out”). And you can do this at design time or runtime.

Disabling Menu Items At Design Time

To disable a menu item at design time, just deselect the Enabled box in the Menu Editor, as shown in Figure 5.16, where we disable the Insert menu item.


Figure 5.16  Disabling a menu item at design time.

Now when the Edit menu is first shown, the Insert item will be disabled.

Disabling Menu Items At Runtime

You can also disable (and enable) menu items at runtime using the item’s Enabled property. You set this property to True to enable a menu item and to False when you want to disable an item.

For example, here’s how we disable the Edit menu’s Insert item when the user clicks it (note that in this program there is then no way for the user to enable it again):

Private Sub mnuEditInsert_Click()
    mnuEditInsert.Enabled = False
End Sub

Figure 5.17 shows the result—we’ve disabled the Insert menu item.


Figure 5.17  Disabling a menu item at runtime.

Handling MDI Form And MDI Child Menus

You’ve created your new program, the SuperWizardTextEditor, and made it an MDI program. But now there’s a call from the Testing Department—users are getting confused. Why is the Edit menu still visible when no documents are open to edit? Can you fix this?

Yes you can. Visual Basic lets you specify two menus in an MDI program, one for the MDI form and one for the MDI child form (and more if you have several types of MDI child forms). If the MDI form has a menu and the MDI child form has no menu, the MDI form’s menu is active at all times.

If, on the other hand, the MDI child form has a menu, that menu takes over the MDI form’s menu system any time one or more of those child forms is open. What this means in practice is that you give the MDI form a rudimentary menu system (typically just File and Help menus) and save the full menu system (like File, Edit, View, Insert, Format, Tools, Window, Help, and so on) for the child windows to ensure the full menu system is on display only when documents are open and those menus apply.

For example, you might add just this simple menu system to the MDI form in an MDI program. Note that you should, at a minimum, give the user some way to open a new or existing document, and you should provide access to Help:

File
....New
....Open
Help
....Contents

Here’s an example of a full menu system you might then give to the MDI child form, which will take over the main MDI form’s menu system when a child form is open:

File
....New
....Open
....Save
....Save As
Edit
....Cut
....Copy
....Paste
Tools
....Graphics Editor
....Charts Editor
....Exporter
Help
....Contents


TIP:  If the user closes all documents at any time, the MDI form’s menu system becomes active again—it’s only when MDI child forms are open that their menus take over the main menu system.

Adding A List Of Open Windows To An MDI Form’s Window Menu

You might have noticed that Window menus in professional MDI programs include a list of open MDI child windows, and you can select which child is active by selecting from this list. You can add that to your program by adding all the code yourself, but there’s an easier way—you can set a menu’s WindowList property.

Setting a menu’s WindowList property to True adds a list of windows to that menu, and you can set the WindowList property in the Menu Editor simply by selecting a checkbox, as shown in Figure 5.18.


Figure 5.18  Adding a window list to a Window menu.

Now when the program runs, the menu you added a window list to will indeed display a list of open windows, separated from the rest of the menu items with a menu separator, as shown in Figure 5.19.


Figure 5.19  Our window list is active.

You’ve added a touch of professionalism to your program with a single mouse click.

Making Menus And Menu Items Visible Or Invisible

The Field Testing Department is on the phone again. Someone there doesn’t like the look of the 30 disabled menu items in the Edit menu. You explain that those items just don’t apply in most cases, so they should be disabled. The Field Testing people suggest you just remove those items from the Edit menu until they can be used. How does that work?

Like other Visual Basic controls, menus and menu items have a Visible property, and you can set that property to True to make a menu or menu item visible, and to False to make it invisible (and so remove it from a menu bar or menu).

For example, you might have an item in the File menu: “Connect to the Internet”, which is inappropriate in a computer that has no way to connect to the Internet. You can make that item disappear from the File menu by setting its Visible property to False, as we do here after checking some hypothetical variable blnCanConnect :

If blnCanConnect Then
    mnuFileInternet.Visible = True
Else
    mnuFileInternet.Visible = False
End If

Making menus and menu items visible or invisible is often a better alternative to displaying menus with too many disabled items (which can frustrate the user and make a program seem inaccessible).

Creating And Displaying Pop-Up Menus

Pop-up menus—those menus that appear when you right-click a form—have become very popular these days, and we can add them to Visual Basic programs.

Creating A Pop-up Menu

To create a new pop-up menu, just use the Menu Editor as shown in Figure 5.20, where we create a new menu named Popup (you can use whatever caption you want for the menu; the caption does not appear when the popup menu appears—only the items in the menu appear). The menu has two items in it: Message (displays a message box) and Beep (beeps).


Figure 5.20  Designing a pop-up menu.

Note that we set this menu’s Visible property to False to make sure we don’t display it in the menu bar.

We’ve created our pop-up menu now—but it doesn’t appear in the menu bar. How can we add code to the two items in that menu?


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.