Search In Site

25 June, 2013

Creating And Displaying Pop-Up Menus

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, 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).
Note that we set this menus 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?
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, 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

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.

0 comments:

Post a Comment

Dear Visitors All The Tricks And Hacks Posted Here Are Only For Knowledge Purpose.Don't Use These for Illegal Operations.

 
Twitter Bird Gadget