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.N | Property | Description |
---|---|---|
1 | Checked | Gets or sets a value indicating whether the ToolStripMenuItem is checked. |
2 | CheckOnClick | Gets or sets a value indicating whether the ToolStripMenuItem should automatically appear checked and unchecked when clicked. |
3 | CheckState | Gets or sets a value indicating whether a ToolStripMenuItem is in the checked, unchecked, or indeterminate state. |
4 | Enabled | Gets or sets a value indicating whether the control is enabled. |
5 | IsMdiWindowListEntry | Gets a value indicating whether the ToolStripMenuItem appears on a multiple document interface (MDI) window list. |
6 | ShortcutKeyDisplayString | Gets or sets the shortcut key text. |
7 | ShortcutKeys | Gets or sets the shortcut keys associated with the ToolStripMenuItem. |
8 | ShowShortcutKeys | Gets 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.N | Event | Description |
---|---|---|
1 | CheckedChanged | Occurs when the value of the Checked property changes. |
2 | CheckStateChanged | Occurs 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.
- 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
- Add a button control on the form with text 'Show Project'.
- 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:
Clicking on the Show Project button displays the project menu:
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.
- 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
- Add a button control on the form with text 'Enable Project'.
- 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:
Clicking on the Enable Project button enables the project menu:
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.
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.
No comments:
Post a Comment