Similarly, the Click event of the Show menu option can be implemented to call the Show() method of the MyTextBox object.
We now need to write some Visual Basic code to call the Hide() method of the MyTextBox object as follows: Private Sub HideToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Double click on the Hide menu option to display the Click event procedure code for this menu option. Once selected, a representation of the context menu will appear in the form. In Visual Studio, select the TextBoxMenu object in the panel beneath the form.
This is achieved by implementing Click event procedures for the hide and show menu items.
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. Programming Visual Basic Context Menu Options
To complete this phase of the tutorial, add menu items labeled Hide and Show. The difference between a MenuStrip control and a ContextMenuStrip control is that a MenuStrip control is associated with the Windows Form whereas. It is also possible to define an image to be displayed for the menu option. To add a MainMenu component, open the Forms Toolbox and add the MainMenu component to your form. The MainMenu control allows you to create, add, and modify menus and menu bars and set their properties in the Properties window. 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. To add menus to your VB.NET application in design-time, you need to add a MainMenu component to your form. With the exception of separators and MenuItems, these items should not be placed in menus because they violate the rules of good GUI design. It is possible to add ComboBoxes, Separators, TextBoxes and MenuItems to a context menu. To add an item other than a menu option, click on the small down arrow which appears in the text box. Each time you enter a new item a new Type Here field will appear. You will notice also, that a representation of the context appears in the form area:Īdd items to the menu simply by typing in the Type Here fields. Ill cover the following topics in the code samples below: SystemEventArgs, Click, ContextMenu, ContextMenuStrip. With the object selected, change the name to TextBoxMenu by changing the Name property in the Properties panel. Add ContextMenuStrip in button click event. To display the menu in the form, or edit properties of the object, simply click on this instance of the object. 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. From the Visual Studio Toolbox, double click on the ContextMenuStrip object. 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.