|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
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, well just make the Beep item beep and the Message item display a message box acknowledging the users 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 menubut 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:
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. Heres 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
Thats itthe result appears in Figure 5.21. Now were using pop-up menus in Visual Basic.
Adding And Deleting Menu Items At RuntimeWeve all seen menus that change as a program runs, and that can be a sophisticated effect. Its 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, well just add new itemsItem 1, Item 2, and so onto 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.
The Items item is actually a placeholder for the items well 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 placeholderwe dont want it to be visible before the user adds items to this menuso 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 Well 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 its 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
Thats itnow the File menu can grow as you like, as shown in Figure 5.23.
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 MenusYou can even add bitmaps to Visual Basic menu items, although you cant use Visual Basic directly to do that. To see how to do that, well create an example that will load in a small bitmap file, image.bmp, and display it in a menu item.
|
|
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. |