Creating Context Menus in Visual Basic

Revision as of 15:10, 25 July 2007 by Neil (Talk | contribs) (Adding Context Menus to a Visual Basic Form)

Revision as of 15:10, 25 July 2007 by Neil (Talk | contribs) (Adding Context Menus to a Visual Basic Form)

Visual basic Context menus are the menus that pop up when the user clicks with the right hand mouse button over a control or area in a form. They are called context menus because the menu is usually specific to the object over which the mouse was clicked. Context menus are also sometimes referred to as Popup menus or Shortcut menus.

This chapter will provide an overview of the steps necessary to create and programme Context menus in Visual Basic. For information on creating Top-level menus see the Creating Top-Level Menus in Visual Basic chapter of this book.

Adding Context Menus to a Visual Basic Form

Context menus are added to a form in Visual Basic using the ContextMenuStrip object and associating it with a particular control. Begin by creating a new Windows Application project in Visual Studio. Next, add a RichTextBox component to the form so that the form appears as follows and name it MyTextBox:

File:Exampl.jpg

The purpose of this example is to create a context menu on form which will contain options to hide and show the MyTextBox' object. From the Visual Studio Toolbox, double click on the ContextMenuStrip object. The object will be added to the form. You will notice, however, that since the menu is not located at any specific position in the form, it is located in the panel beneath the form. To display the menu in the form, or edit properties of the object, simply click on this instance of the object. With the object selected, change the name to TextBoxMenu by changing the Name property in the Properties panel. You will notice also, that a representation of the context appears in the form area:

File:Exampl.jpg

Add items to the menu simply by typing in the Type Here fields. Each time you enter a new item a new Type Here field will appear. To add an item other than a menu option, click on the small down arrow which appears in the text box. It is possible to add ComboBoxes, Separators, TextBoxes and MenuItems to a context menu. With the exception of separators and MenuItems, these items should not be placed in menus because they violate the rules of good GUI design.

Once an item has been added to the menu right clicking on the item provides a list of properties which may be changed, such as making an item checkable, or disabling an item so that it cannot be selected by the user. It is also possible to define an image to be displayed for the menu option.

To complete this phase of the tutorial, add menu items labeled Hide and Show.

Now that we have designed our context menu we need to associate it with the form object. To do this, select the form in Visual Studio and scroll through the properties in the Properties panel until you find the ContextMenuStrip property. Click on the down arrow in the value field and select TextBoxMenu to associate this menu with the form.

Press F5 to build and run the application. When the application loads, right click with the mouse on any part of the form to display the context menu.

Programming Visual Basic Context Menu Options