Changes

Creating Context Menus in Visual Basic

6 bytes added, 14:49, 9 April 2009
no edit summary
[[Image:visual_basic_textbox_context.jpg]]
The purpose of this example is to create a context menu on the 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:
[[Image:visual_basic_new_context_menu.jpg]]
== Programming Visual Basic Context Menu Options ==
Now that we have designed the context menu, and associated it with the form object, we need to write some Visual Basic code to cause the ''MyTextBox'' object to hide and show depending on the menu selection. This is achieved by implementing ''Click'' event procedures for the hide and show menu items. In Visual Studio, select the ''TextBoxMenu'' object in the panel beneath the form. Once selected, a representation of the context menu will appear in the form. Douible Double click on the ''Hide'' menu option to display the Click event procedure code for this menu option. We now need to write some Visual Basic code to call the ''Hide()'' method of the ''MyTextBox'' object as follows:
<pre>
== Compiling and Running the Application ==
Compile and run the application by pressing the '''F5''' key. Right and right click anywhere on the form to invoke the context menu and select ''Hide''. The TestBox will disappear from the form. Right click again and select ''Show'' to once again display the TextBox:
[[Image:visual_basic_context_example_running.jpg]]