|
Menus
Everyone should be familiar with Menus. Menus (File, Edit, Format etc in all windows
applications) are those that allow us to make a selection when we want to perform
some action with the application, for example, to format the text, open a new file,
print and so on. In VB .NET MainMenu is the container for
the Menu structure of the form. Menus are made of MenuItem objects
that represent individual parts of a menu (like File->New, Open, Save, Save As
etc). The two main classes involved in menu handling are, MainMenu and MenuItem. The
MainMenu class let's us assign objects to a form's menu class and MenuItem is the
class which supports the items in a menu system. Menus like File, Edit, Format etc
and the items in those Menus are supported by this MenuItem class. It's this MenuItem's click event
that makes these Menus work. For a MenuItem to be displayed, we need to add it to
a MainMenu object.
Event of the MenuItem
The default event of the MenuItem is the Click event which looks like this in code:
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles MenuItem1.Click
End Sub
|
Notable properties of the MenuItem class are summarized below.
Under the Miscellaneous Section of the properties window:
Checked: Default value is set to False. Changing it to True
makes a checkmark appear towards the left of the Menu.
DefaultItem: Default value is set to False. Changing it
to True makes this menu item default menu item.
RadioCheck: Changing it to True makes a menu item display
a radio button instead of a checkmark.
Shortcut: Enables to set a short cut key from a list of
available shortcuts for the menu item.
Working with Menus
Creating Menus is simple. Drag a MainMenu component from the toolbar onto the
form. When you add a MaiuMenu component to the form it appears in the component tray
below the form. Windows form designer will add the MenuItem's for this by default,
you need not add this. Once when you finish adding a MainMenu component to the form
you will notice a "TypeHere" box towards the top-left
corner of the form. To create a menu all you have to do is click on the "TypeHere"
text which opens up a small textbox allowing you to enter text for the menu. You can
view that in the image below. You can use the arrow keys on the keyboard to create
a submenu or add other items to that menu or click on the first menu item and use
the left/right arrow keys on the keyboard to create a new menu item. That's all it
takes to add a menu to the form.
Working with an example
Let's work with an example to understand Menus. Drag a MainMenu and a TextBox onto
the form. In the "Type Here" part, type File and under file type "New" and "Exit".
Our intention here is to display "Welcome to Menus" in the TextBox when "New" is clicked
and close the form when "Exit" is clicked. The Menu which we will create should look
like this File->New, Exit (New and Exit below File). The code for that looks
like this:
Public Class Form3 Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e_
As System.EventArgs)_ Handles MenuItem2.Click
TextBox1.Text = "Welcome to Menus"
End Sub
Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)_
Handles MenuItem3.Click
Me.Close()
'Me refers to the current object (form)
End Sub
End Class
|
Next>>Menu Separator, Cloning Menus
|