Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 30,961 times

Contents

Related Categories

Menus - Popup menus

Popup menus

VB allows you to easily create a popup menu like those you see in Explorer when you right click. All you need to do is create a menu, containing all the items you want displayed on the popup menu. Then, set the menus visible property to false, so it is not displayed on the forms menu bar. Now add the following code to the MouseUp event of any control on which you want the popup menu to be displayed when it is right clicked.

If Button = vbRightButton Then
    ' Set the flags
    vFlags = vbPopupMenuRightButton
    ' Display the menu
    PopupMenu mnuEdit, vFlags
End If

The popupmenu function has the following parameters:

PopupMenu PopupMenu, Flags, X, Y, DefaultMenu

There are a few flags which you can set before displaying the menu. These are described below:

vbPopupMenuRightButton

Popup menu disappears when whether the left or right mouse button is clicked off the menu

vbPopupMenuLeftButton

Popup menu only disappears when the left mouse button is clicked off the menu

vbPopupMenuCenterAlign

Aligns the menu in the middle of where the mouse is clicked

vbPopupMenuLeftAlign

Aligns the menu to the left of where the mouse is clicked.

vbPopupMenuRightAlign

Aligns the menu to the right of where the mouse is clicked.

The X and Y parameters are optional, but you could use them to display the menu at a fixed point (ie next to a button that is clicked to display it).

You can also display a default item in bold, by passing the menu item you want in its DefaultMenu parameter.

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • help with the code

    Posted by minor on 28 Apr 2003

    In this tutorial i dosent tell you the code for the menu. and im also very new to vb so if someone could tell me what i need to do for the code that would be great.

  • VFlags variable not defined?

    Posted by planethax on 17 Nov 2002

    I have made a menu with1 1 Item (about) set it to enable - unchecked visable
    made a click event(msg box)

    then in my form mouse up event I put
    [code]
    Private Sub Form_MouseUp(Button As Integer, ...

  • Posted by James Crowley on 20 May 2002

    sorry 'bout that... there was an old style link lurking in there... I have corrected the link on

    http://www.developerfusion.com/show/15/10/, which now points to

    http://www.developerfusion.com/sh...

  • example

    Posted by Rollershade on 20 May 2002

    i click view an example and no example???
    i really want to change the look of my menu,change color or nything? ny ideas?