Showing posts with label VB.Net Advanced Form. Show all posts
Showing posts with label VB.Net Advanced Form. Show all posts

Saturday, 14 September 2013

VB.Net - ContextMenuStrip Control

The ContextMenuStrip control represents a shortcut menu that pops up over controls, usually when you right click them. They appear in context of some specific controls, so are called context menus. For example, Cut, Copy or Paste options.
This control is associated a context menu with other menu items by setting that menu item's ContextMenuStrip property to the ContextMenuStrip control you designed.
Context menu items can also be disabled, hidden or deleted. You can also show a context menu with the help of the Show method of the ContextMenuStrip control.
The following diagram shows adding a ContextMenuStrip control on the form:
VB.Net ContextMenuStrip Control

Properties of the ContextMenuStrip Control

The following are some of the commonly used properties of the ContextMenuStrip control:
S.NPropertyDescription
1SourceControlGets the last control that displayed the ContextMenuStrip control.

Example

In this example, let us add a content menu with the menu items Cut, Copy and Paste.
Take the following steps:
  1. Drag and drop or double click on a ControlMenuStrip control, to add it to the form.
  2. Add the menu items, Cut, Copy and Paste to it.
  3. Add a RichTextBox control on the form.
  4. Set the ContextMenuStrip property of the rich text box to ContextMenuStrip1 using the properties window.
  5. Double the menu items and add following codes in the Click event of these menus:
Private Sub CutToolStripMenuItem_Click(sender As Object, e As EventArgs) _
Handles CutToolStripMenuItem.Click
   RichTextBox1.Cut()
End Sub

Private Sub CopyToolStripMenuItem_Click(sender As Object, e As EventArgs) _
Handles CopyToolStripMenuItem.Click
   RichTextBox1.Copy()
End Sub

Private Sub PasteToolStripMenuItem_Click(sender As Object, e As EventArgs) _
Handles PasteToolStripMenuItem.Click
   RichTextBox1.Paste()
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:
Context Menu Strip Example
Enter some text in the rich text box, select it and right-click to get the context menu appear:
Context Menu Strip Example
Now you can select any menu items and perform cut, copy or paste on the text box.

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

VB.Net - MenuStrip Control

The MenuStrip control represents the container for the menu structure.
The MenuStrip control work as the top-level container for the menu structure. The ToolStripMenuItem class and the ToolStripDropDownMenu class provide the functionalities to create menu items, sub menus and drop-down menus.
The following diagram shows adding a MenuStrip control on the form:
VB.Net MenuStrip Control

Properties of the MenuStrip Control

The following are some of the commonly used properties of the MenuStrip control:
S.NPropertyDescription
1CanOverflowGets or sets a value indicating whether the MenuStrip supports overflow functionality.
2GripStyleGets or sets the visibility of the grip used to reposition the control.
3MdiWindowListItemGets or sets the ToolStripMenuItem that is used to display a list of Multiple-document interface (MDI) child forms.
4ShowItemToolTipsGets or sets a value indicating whether ToolTips are shown for the MenuStrip.
5StretchGets or sets a value indicating whether the MenuStrip stretches from end to end in its container.

Events of the MenuStrip Control

The following are some of the commonly used events of the MenuStrip control:
S.NEventDescription
1MenuActivateOccurs when the user accesses the menu with the keyboard or mouse.
2MenuDeactivateOccurs when the MenuStrip is deactivated.

Example

In this example, let us add menu and sub-menu items.
Take the following steps:
  1. Drag and drop or double click on a MenuStrip control, to add it to the form.
  2. Click the Type Here text to open a text box and enter the names of the menu items or sub-menu items you want. When you add a sub-menu, another text box with 'Type Here' text opens below it.
  3. Complete the menu structure shown in the diagram above.
  4. Add a sub menu Exit under the File menu
VB.Net MenuStrip Example
Double-Click the Exit menu created, and add the following code to the Click event ofExitToolStripMenuItem:
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) _
   Handles ExitToolStripMenuItem.Click
   End
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 MenuStrip Example
Click on the File -> Exit to exit from the application:
VB.Net MenuStrip Example