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


What the user wants appears in Figure 5.10. As you can see in that figure, the Colors item in the Edit menu has a small arrow at the right. This indicates that there’s a submenu attached to this menu item. Selecting the menu item opens the submenu, as also shown in Figure 5.10. As you can see, submenus appear as menus attached to menus.


Figure 5.10  A program with a submenu.

Submenus let you organize your menu system in a compact way, and adding them to a program is simple. For example, let’s say you started this way, with a Red, Green, and Blue menu item in the Edit menu:

Edit
....Cut
....Copy
....Paste
....Red
....Green
....Blue
....Select All

To put those items in a submenu, we first add a name for the submenu—say, Colors:

Edit
....Cut
....Copy
....Paste
....Colors
....Red
....Green
....Blue
....Select All

All that’s left is to indent (using the right arrow in the Menu Editor) the items that should go into that submenu (note that they must appear just under the submenu’s name):

Edit
....Cut
....Copy
....Paste
....Colors
........Red
........Green
........Blue
....Select All

That’s it—close the Menu Editor.

You add code to submenu items in the same way that you add code to menu items—just click them to open the corresponding event-handling function and add the code you want, as we’ve done here to report the user’s color selection:

Private Sub mnuEditColorsBlue_Click()
    MsgBox ("You selected Blue")
End Sub

Private Sub mnuEditColorsGreen_Click()
    MsgBox ("You selected Green")
End Sub

Private Sub mnuEditColorsRed_Click()
    MsgBox ("You selected Red")

Creating Immediate (“Bang”) Menus

Sometimes you’ll see immediate menus (also called “bang” menus) in menu bars. These are special menus that don’t open—when you merely click them in the menu bar, they execute their associated command. The name of these menus is followed with an exclamation mark (!) like this: Download! When you click the Download! item in the menu bar, the downloading process starts at once, without opening a menu at all.

Now that toolbars are so common, one sees fewer immediate menus (that is, toolbars act very much like immediate menus are supposed to work), but some programmers still use them And because they’re easy to create, we’ll cover them here.

To create an immediate menu, just add a menu, such as Download! (don’t forget to add exclamation point on the end of “Download” in the Caption property, but not in the Name property), and don’t give it any menu items. Instead, place the code you want to run in the Click event handler for the menu itself:

Private Sub mnuDownload_Click()
    MsgBox ("Downloading from the Internet...")
End Sub

That’s all you need. Now when the user selects the Download! menu, this code will be executed. We’re about to execute the Download! immediate menu in Figure 5.11. Note that there is no menu opening, even though the Download! item in the menu bar is selected.


Figure 5.11  Selecting an immediate menu.

Using The Visual Basic Predefined Menus

You can use the Visual Component Manager to add a predefined menu to a form (note that not all versions of Visual Basic come with the Visual Component Manager). As you can see in the Visual Component Manager’s Visual Basic|Templates|Menus folder, as shown in Figure 5.12, six predefined menus are available. These menus include a File menu, an Edit menu, a Help menu, a Window menu, and so on. To add one of these menus to a form, just select the form and double-click the menu in the Visual Component Manager.


Figure 5.12  Selecting a predefined menu.

For example, we can add a predefined File menu to a form this way. The result appears in Figure 5.13.


Figure 5.13  Using a predefined menu.

Adding a predefined menu also adds code to the form. For example, here’s the skeletal code that’s added when you add a predefined File menu:

Private Sub mnuFileNew_Click()
  MsgBox "New File Code goes here!"
End Sub

Private Sub mnuFileOpen_Click()
  MsgBox "Open Code goes here!"
End Sub

Private Sub mnuFilePrint_Click()
  MsgBox "Print Code goes here!"
End Sub

Private Sub mnuFilePrintPreview_Click()
  MsgBox "Print Preview Code goes here!"
End Sub

Private Sub mnuFilePrintSetup_Click()
  MsgBox "Print Setup Code goes here!"
End Sub

Private Sub mnuFileProperties_Click()
  MsgBox "Properties Code goes here!"
End Sub

Private Sub mnuFileSave_Click()
  MsgBox "Save File Code goes here!"
End Sub

Private Sub mnuFileSaveAll_Click()
  MsgBox "Save All Code goes here!"
End Sub

Private Sub mnuFileSaveAs_Click()
  MsgBox "Save As Code goes here!"
End Sub

Private Sub mnuFileSend_Click()
  MsgBox "Send Code goes here!"
End Sub


TIP:  If you don’t have the Visual Component Manager, you can add a form with a predefined menu to a project. Select Project|Add Form, click the Existing tab, and open the Menus folder to find the possible menu forms to add to your project.

Adding A Checkmark To A Menu Item

When you want to toggle an option in a program, such as Insert mode for entering text, it’s easy to add or remove checkmarks in front of menu items. Displaying a checkmark gives visual feedback to the user about the toggle state of the option, and there’s two ways to add checkmarks to menu items: at design time and at runtime.

Adding Checkmarks At Design Time

To add a checkmark to a menu item at design time, you simply select the Checked box in the Menu Editor, as shown in Figure 5.14, where we add a checkmark to the Edit menu’s Insert item.


Figure 5.14  Adding a checkmark to a menu item at design time.

Now when the Edit menu is first displayed, the Insert item will appear checked.

Adding Checkmarks At Runtime

You can also set checkmarks at runtime using a menu item’s Checked property. For example, here’s how we toggle the Insert item’s checkmark each time the user selects that item; setting Checked to True places a checkmark in front of the item, and to False removes that checkmark:

Private Sub mnuEditInsert_Click()
    Static blnChecked As Boolean
    blnChecked = Not blnChecked
    mnuEditInsert.Checked = blnChecked
End Sub

Running this code toggles a checkmark in front of the Insert item, as shown in Figure 5.15.


Figure 5.15  Adding a checkmark to a menu item at runtime.


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.