Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Visual Basic
Expertise: Beginner
Aug 6, 1997

Providing Context Menus for Your UI Objects

Much of the ease of use of Windows 95 comes from the fact that its user interface objects have their own context menus, which can be accessed through a simple right-click of the mouse. In keeping with this theme, you can provide context menus for the interface objects in your applications too. Making and responding to a context menu is a pretty straightforward and simple process. These steps illustrate how this is done using a standard list box called lstSample as the interface object:

1. Define the context menu. The context menu is really a standard menu item which has submenu items just like your Help menu item would. Unlike your Help menu item, however, a context menu item will have its Visible property set to False so that the user never sees it on the form's menu. For this example, open a new form and use the Menu Editor to make a new top-level menu item and give it the name mnu_lstSampleContextMenu. The caption will never be seen by the user, but should be something descriptive that reminds you what the menu is used for, such as "Context Menu For lstSample Control." Set the Visible check box for this menu item to False. Now, define the sub-menu items that will appear when the user right-clicks on the control: "&Clear," "Clear A&ll," "&Add Item," "&Remove Item," and so forth.

2. Write the code that will show the context menu when the user right-clicks the control. This is done by invoking VB's PopupMenu method in the control's _MouseDown event. Here is a code sample:

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

        ' if the user right clicked on 
        ' control then show the popup menu 
        ' for this control
        If Button And vbRightButton Then _
                PopupMenu _
                mnu_lstSampleContextMenu

End Sub




3. All that is left to do is to write the code in the click event for each of the context menu's submenu items. The PopupMenu method can also do neat things like bold a menu item, place the menu at a specific location, and so forth. For more information on the PopupMenu method, see the VB help file.

Hassan Davis
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date