|
Menus
Seperating Menu Items
We can separate menu items with a seperator. A separator is a horizontal line between
items on a menu. We can use separator bars to divide menu items into groups on menus
that contain multiple items. You add a separator to menus by entering a hypen (-) as
the text of a menu item. It will be displayed as a separator. The image below displays
a separator bewteen the Close and Exit menu items.
Cloning Menus
Cloning menus is making a copy of existing menu items. For example, we can clone the
File menu item displayed in the image below to serve as a context menu for a
control. To clone a menu we should use the CloneMenu method.
The CloneMenu method creates a copy of the specified menu and all of it's members.
This includes their properties and event handlers. Thus, all the events that are handled
by the menu item will be handled by the cloned menu. The cloned menu can then be assigned
to a control.
Example
On a new form drag a MainMenu component, ContextMenu component and a Button. Click
on the MainMenu component and under the "type here" boxes enter the menu items as
shown in the image below. The menu items you entered for MainMenu will appear as a
context menu (right-click) for the button control. For the purpose of explanation
minimum functionality is added to the menu items. The image below displays the form
in design view.
Code to clone the MainMenu component
Public Class Form4 Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
#End Region
Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
Contextmenu1.MenuItems.Add(MenuItem1.CloneMenu)
'cloning the filemenuitem and filling contextmenu1 with the cloned item
Button1.ContextMenu = Contextmenu1
'assigning the new contextmenu to button1
End Sub
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MenuItem2.Click
MessageBox.Show("You clicked New")
End Sub
Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MenuItem3.Click
MessageBox.Show("You clicked Open")
End Sub
Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MenuItem4.Click
MessageBox.Show("You clicked Close")
End Sub
Private Sub MenuItem6_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MenuItem6.Click
Me.Close()
End Sub
End Class
|
The image below displays output from the code above. When you right-click the button
and click on any menu item, it performs the same functionality as it would
for the File menu item.
Shortcut Keys
You can assign shortcut keys to enable instant access to menu commands. To assign
a shortcut to the menu item, in the properties window select the shortcut property
and choose the appropriate shortcut key combination from the drop-down menu.
|