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


You reach those two items, mnuPopupMessage and mnuPopupBeep, in the code window. Double-click the form now to open the code window. The left drop-down box in the code window lists all the objects in the form, so find mnuPopupMessage and mnuPopupBeep and add event-handling functions to their Click events:

Private Sub mnuPopupBeep_Click()

End Sub

Private Sub mnuPopupMessage_Click()

End Sub

Here, we’ll just make the Beep item beep and the Message item display a message box acknowledging the user’s action:

Private Sub mnuPopupBeep_Click()
    Beep
End Sub

Private Sub mnuPopupMessage_Click()
    MsgBox ("You selected the Message item")
End Sub

That completes the design of the pop-up menu—but how do we display it when the user right-clicks the form?

Displaying A Pop-Up Menu

We want to check for right mouse button events, so add a MouseDown event handler to our program using the code window now:

Private Sub Form_MouseDown(Button As Integer, Shift As Integer,_
    X As Single,Y As Single)
End Sub

You can tell which mouse button went down by comparing the Button argument to these predefined Visual Basic constants:

  vbLeftButton = 1
  vbRightButton = 2
  vbMiddleButton = 4

This means we check for the right mouse button:

Private Sub Form_MouseDown(Button As Integer, Shift As Integer,_
    X As Single, Y As Single)
    If Button = vbRightButton Then
...
    End If
End Sub

If the right mouse button did go down, we display the pop-up menu with the PopupMenu method:

[object.]PopupMenu menuname [, flags [,x [, y [, boldcommand ]]]]

Here, menuname is the name of the menu to open, the possible values for the flags parameter appear in Table 5.1, x and y indicate a position for the menu, and boldcommand is the name of the one (but no more than one) menu item you want to appear bold. Here’s how we use PopupMenu:

Private Sub Form_MouseDown(Button As Integer, Shift As Integer,_
    X As Single, Y As Single)
    If Button = vbRightButton Then
        PopupMenu Popup
    End If
End Sub
Table 5.1 Pop-UpMenu constants.
Constant Does This
vbPopupMenuLeftAlign Default. The specified x location defines the left edge of the pop-up menu.
vbPopupMenuCenterAlign The pop-up menu is centered around the specified x location.
vbPopupMenuRightAlign The specified x location defines the right edge of the pop-up menu.
vbPopupMenuLeftButton Default. The pop-up menu is displayed when the user clicks a menu item with the left mouse button only.
vbPopupMenuRightButton The pop-up menu is displayed when the user clicks a menu item with either the right or left mouse button.

That’s it—the result appears in Figure 5.21. Now we’re using pop-up menus in Visual Basic.


Figure 5.21  Our pop-up menu at work.

Adding And Deleting Menu Items At Runtime

We’ve all seen menus that change as a program runs, and that can be a sophisticated effect. It’s also impressive if the menu can change in response to user input (for example, adding a new item with the caption “Create Progname.exe”, where Progname is the name given the program). You can add this capability to your program in Visual Basic.

Here, we’ll just add new items—Item 1, Item 2, and so on—to the File menu with the user clicks a button. We start by designing our menu system, giving it a File menu with two items: New and Items, as you can see in Figure 5.22.


Figure 5.22  Designing an extendable menu.

The Items item is actually a placeholder for the items we’ll add to the File menu. Make this item into a control array by giving it an index, 0, in the Index box, as shown in Figure 5.22. This item is just a placeholder—we don’t want it to be visible before the user adds items to this menu—so set its Visible property to False, as also shown in Figure 5.22.

Now add a button to the program, and give it a Click event-handling function:

Private Sub Command1_Click()

End Sub

We’ll keep track of the items in the File menu with a variable named intItemCount, which we increment each time the button is clicked:

Private Sub Command1_Click()
    Static intItemCount
    intItemCount = intItemCount + 1
...
End Sub

To add a new item to the Items control array, we use Load():

Private Sub Command1_Click()
    Static intItemCount
    intItemCount = intItemCount + 1
    Load mnuFileItems(intItemCount)
...
End Sub

Finally, we set the caption of the item to indicate what its item number is, and make it visible:

Private Sub Command1_Click()
    Static intItemCount
    intItemCount = intItemCount + 1
    Load mnuFileItems(intItemCount)
    mnuFileItems(intItemCount).Caption = "Item " & intItemCount
    mnuFileItems(intItemCount).Visible = True
End Sub

You can also add a Click event handler to the Items menu item (because it’s not visible in the menu bar, find mnuFileItems in the code window and add the event handler to it there). This event handler is passed the index of the clicked item in the control array, so we can indicate to the user which item he has clicked:

Private Sub mnuFileItems_Click(Index As Integer)
    MsgBox ("You clicked item " + Str(Index))
End Sub

That’s it—now the File menu can grow as you like, as shown in Figure 5.23.


Figure 5.23  Adding items to a menu at runtime.

To remove items from the menu, just use Unload() statement like this (and make sure you adjust the total item count):

Unload mnuFileItems(intItemCount)

Adding Bitmaps To Menus

You can even add bitmaps to Visual Basic menu items, although you can’t use Visual Basic directly to do that. To see how to do that, we’ll create an example that will load in a small bitmap file, image.bmp, and display it in a menu item.


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.