Working with Menu in VB.NET


Menu is one of the most common elements of Graphical User Interface.
Menu is a one type of control that represents a group of choices to the user and allows user to select any of them according to their requirement.
Using menu user can organizes various options or commands as per their functionality
It helps programmer to organize large number of options in a short and easy way.
It can be attached only with form either SDI or MDI.
It is displayed immediately under the title bar of the form as shown below:
Menu
User can assign access key to each menu item for quick access to the menu item through key board.
User can assign shortcut key to each menu items for accessing them through keyboard.

     

Creating Menu in VB.NET


In order to create a menu double click on the MenuStrip control in the Tool Box as shown below:
Create Menu

As you double click on MenuStrip control a menu bar is added just under the title bar of the form and MenuStrip is added in the component tray under the form as shown below:
Creating Menu
Once Menu Bar is added to the form you can add, edit and delete menu items and submenu as per your requirements.
In order to create Menu Item just double click on “Type Here” TextBox and type name of the Menu Item. As you enter name of Menu item “Type Here” TextBox will appear to the right and bottom of the Menu Item as shown below:
Creating Menu
You can repeat same procedure in order to create Menu Item and Sub Menu as per your requirement.

Properties of MenuStrip Control in VB.NET


Property Purpose

AllowItemReorder

It is used to specify weather user can reorder menu items by holding Alt key or not. It has Boolean value. Default value is false.

BackColor

It is used to get or set back color of the MenuStrip.

Enabled

It is used to specify weather MenuStrip is enabled or not at run time. It has Boolean value. Default value is true.

Font

It is used to set Font Face, Font Style, Font Size and Effects of the text associated with Menu Items of MenuStrip Control.

Items

It represents collection of Menu Items contained in Menu Strip control.

LayoutStyle

It is used to get or set Layout Style of Menu Strip Control. It has following 5 options:
(1) Stack with Overflow
(2) Horizontal Stack With Overflow
(3) Vertical Stack With Overflow
(4) Flow
(5) Table

ShowItemToolTips

It is used to specify weather Tool Tip text will be displayed for each menu item or not when mouse is over that menu item. It has Boolean value. Default value is false.

TextDirection

It is used to get or set value which determines direction of text in each menu Item. It has following 3 options:
(1) Horizontal:  
(2) Vertical90:  
(3) Vertical270:

Visible

It is used to specify weather MenuStrip is visible or not at run time. It has Boolean value. Default value is true.

Assigning Access key to Menu Items


Access key allows user to select menu item from the keyboard using Alt key. Access key is combinations of Alt key and other key (Alt + Other key).
In order to select particular Menu Item User has to press Alt key and then press other key which is defined as access key.
In order to assign Access key to Menu Item just precede the character by & as shown below:
Menu

In above figure character N is defined as Access key for Menu Item New. User can access Menu Item New by pressing Alt + N key.
A character which is defined as an access key for menu item is displayed with underline.
Hence & is used to assign Access Key to particular character in Menu Item, we can not display & in Menu Item directly. In order to display & in Menu Item we have to precede it with another &. For Example to display Save & Close in Menu Item we have to write Save && Close in text property of Menu Item.

Assigning Shortcut Key to Menu Item


Shortcut Key allows user to perform action associated with particular Menu Item using keyboard. Thus using concept of Shortcut Key user can fire action associated with particular Menu Item using keyboard.
Shortcut Key allows user to perform action with a single keystroke.
Shortcut Key is a combination of (Alt, Shift, Ctrl) key and other key as shown in the figure below:
Menu
In order to assign Shortcut Key to particular Menu Item following properties are used:

Property Purpose

ShortcutKeys

It is used to assign Shortcut Key to Menu Item.

ShowShortcutKeys

It is used to specify weather Shortcut Key is displayed beside Menu Item or not. It has Boolean value. Default value is False.

ShortcutKeyDisplayString

It is used to get or set string that is display instead of Shortcut Key.


Adding checkmarks to Menu Item


User can add checkmark to the left side of Menu Item to indicate that the Menu Item is selected or not.
Menu
In order to add checkmark to particular menu item following properties are used:

Property Purpose

Checked

It is used to specify weather checkmark will be displayed to the left side of Menu item or Not. It has Boolean value. Default is False.

CheckOnClick

It is used to specify weather Menu Item will toggle (change) its state or not when it is clicked. It has Boolean value. Default value is false.

CheckState

It is used to get or set state of menu item. It can have one of the following 3 values:
(1) Checked
(2) Unchecked
(3) Indeterminate
Default value is Unchecked.

 

Download Projects


Download Programs