Saturday, 14 September 2013

VB.Net - ToolStripMenuItem Control

The ToolStripMenuItem class supports the menus and menu items in a menu system. You handle these menu items through the click events in a menu system.

Properties of the ToolStripMenuItem Control

The following are some of the commonly used properties of the ToolStripMenuItem control:
S.NPropertyDescription
1CheckedGets or sets a value indicating whether the ToolStripMenuItem is checked.
2CheckOnClickGets or sets a value indicating whether the ToolStripMenuItem should automatically appear checked and unchecked when clicked.
3CheckStateGets or sets a value indicating whether a ToolStripMenuItem is in the checked, unchecked, or indeterminate state.
4EnabledGets or sets a value indicating whether the control is enabled.
5IsMdiWindowListEntryGets a value indicating whether the ToolStripMenuItem appears on a multiple document interface (MDI) window list.
6ShortcutKeyDisplayStringGets or sets the shortcut key text.
7ShortcutKeysGets or sets the shortcut keys associated with the ToolStripMenuItem.
8ShowShortcutKeysGets or sets a value indicating whether the shortcut keys that are associated with the ToolStripMenuItem are displayed next to the ToolStripMenuItem.

Events of the ToolStripMenuItem Control

The following are some of the commonly used events of the ToolStripMenuItem control:
S.NEventDescription
1CheckedChangedOccurs when the value of the Checked property changes.
2CheckStateChangedOccurs when the value of the CheckState property changes.

Example

In this example, let us continue with the example from the chapter 'VB.Net - MenuStrip control'. Let us:
  • Hide and display menu items.
  • Disable and enable menu items.
  • Set access keys for menu items
  • Set shortcut keys for menu items.
Hide and Display Menu Items
The Visible property of the ToolStripMenuItem class allows you to hide or show a menu item. Let us hide the Project Menu on the menu bar.
  1. Add the following code snippet to the Form1_Load event:
    Private Sub Form1_Load(sender As Object, e As EventArgs) _
    Handles MyBase.Load
       ' Hide the project menu
       ProjectToolStripMenuItem1.Visible = False
       ' Set the caption bar text of the form.
       Me.Text = "tutorialspoint.com"
    End Sub
  2. Add a button control on the form with text 'Show Project'.
  3. Add the following code snippet to the Button1_Click event:
    Private Sub Button1_Click(sender As Object, e As EventArgs) _
    Handles Button1.Click
       ProjectToolStripMenuItem1.Visible = True
    End Sub
When the above code is executed and run using Start button available at the Microsoft Visual Studio tool bar, it will show following window:
VB.Net ToolStripMenuItem Example
Clicking on the Show Project button displays the project menu:
VB.Net ToolStripMenuItem Example
Disable and Enable Menu Items
The Enabled property allows you to disable or gray out a menu item. Let us disable the Project Menu on the menu bar.
  1. Add the following code snippet to the Form1_Load event:
    Private Sub Form1_Load(sender As Object, e As EventArgs) _
    Handles MyBase.Load
       ' Disable the project menu
       ProjectToolStripMenuItem1.Enabled = False
       ' Set the caption bar text of the form.
       Me.Text = "tutorialspoint.com"
    End Sub
  2. Add a button control on the form with text 'Enable Project'.
  3. Add the following code snippet to the Button1_Click event:
    Private Sub Button1_Click(sender As Object, e As EventArgs) _
    Handles Button1.Click
       ProjectToolStripMenuItem1.Enabled = True
    End Sub
When the above code is executed and run using Start button available at the Microsoft Visual Studio tool bar, it will show following window:
VB.Net ToolStripMenuItem Example
Clicking on the Enable Project button enables the project menu:
VB.Net ToolStripMenuItem Example
Set Access Keys for Menu Items
Setting access keys for a menu allows a user to select it from the keyboard by using the ALT key.
For example, if you want to set an access key ALT + F for the file menu, change its Text with an added & (ampersand) preceding the access key letter. In other words, you change the text property of the file menu to &File.
VB.Net ToolStripMenuItem ExampleVB.Net ToolStripMenuItem Example
Set Shortcut Keys for Menu Items
When you set a shortcut key for a menu item, user can press the shortcut from the keyboard and it would result in occurrence of the Click event of the menu.
A shortcut key is set for a menu item using the ShortcutKeys property. For example, to set a shortcut key CTRL + E, for the Edit menu:
  • Select the Edit menu item and select its ShortcutKeys property in the properties window.
  • Click the drop down button next to it.
  • Select Ctrl as Modifier and E as the key.
VB.Net ToolStripMenuItem Example

No comments:

Post a Comment