Create menus and submenus for
program control
CHAPTER 4:
MENUS Display and use the windows common
dialog boxes
MENU SYSTEMS
A menu system is a collection of commands organized in one or more drop-down
menus
Commonly used when an application has several options for the user to choose from
The menu designer allows you to visually create a custom menu system
For any form in an application
COMPONENTS OF A MENU
SYSTEM
Each drop-down menu has a menu name
Each drop-down menu has a list of actions or
menu commands that can be performed
Some commands may lead to a submenu
Actions may be performed using a key or key
combination called a shortcut key
A checked menu command toggles between the
checked (if on) and unchecked (if off) states
A separator bar helps group similar commands
MENUSTRIP CONTROL
A MenuStrip control adds a menu to a form
Double-click on the MenuStrip icon in the Menus
& Toolbars section of the Toolbox
MENUSTRIP CONTROL
The MenuStrip control is displayed in the component tray (bottom of
Design window)
A MenuStrip can have many ToolStripMenuItem objects:
Each represents a single menu command
Name property - used by VB to identify it
Text property – text displayed to the user
HOW TO USE THE MENU
DESIGNER
Select the MenuStrip Control
The menu designer appears on the form in
the location that the menu system will
appear
TOOLSTRIPMENUITEM
OBJECT NAMES
It is recommended that you change the
default value of the Name property so that
it
Begins with the mnu prefix
Reflects the Text property and position in
the menu hierarchy
mnuFile
mnuFileSave
mnuFilePrint
mnuFileExit
SHORTCUT KEYS
Combination of keys that cause a menu
command to execute
Ctrl + C to copy an item to the clipboard
Set with the ShortcutKeys property
Displayed only if the ShowShortcut property is
set to True
CHECKED MENU ITEMS
Turns a feature on or off
For example, an alarm for a clock
To create a checked menu item:
Set CheckOnClick property to True
Set Checked property to True if feature
should be on when the form is initially
displayed
If mnuSettingsAlarm.Checked = True Then
MessageBox.Show("WAKE UP!")
End If
CHECKED MENU ITEMS
A menu item is grayed out (disabled) with the
Enabled property, for example:
Paste option is initially disabled and only enabled after
something is cut or copied
Code initially disables the Paste option
DISABLED MENU
mnuEditPaste.Enabled = False
ITEMS
Following a cut or copy, Paste is enabled
mnuEditPaste.Enabled = True
Disabled
DISABLED MENU ITEMS
SEPARATOR BARS
Right-click Menu
Or type a hyphen
item, select Insert
(-) in the menu
then select
item
Separator
A separator bar
will be inserted
above the menu
item
Separator Bar
SEPARATOR BARS
SUBMENUS
When selecting a menu item in the
menu designer, a Type Here box
appears to the right
Begin a submenu by setting up
this menu item
If a menu item has a submenu, a
solid right-pointing arrow will
be shown
INSERTING, DELETING, AND REARRANGING MENU ITEMS
To insert a new menu item
Right-click an existing menu item
Select Insert then MenuItem from pop-up menu
A new menu item will be inserted above the existing menu item
To delete a menu item
Right-click on the item
Choose Delete from the pop-up menu
Or select the menu item and press the Delete key
To rearrange a menu item
Simply select the menu item in the menu designer and drag it to the desired location
TOOLSTRIPMENUITEM CLICK EVENT
Menus and submenus require no code
Commands must have a click event procedure
Double-click on the menu item
Event procedure created in the Code window
Programmer supplies the code to execute
Suppose a menu system has a File menu with an Exit command named
mnuFileExit
Private Sub mnuFileExit_Click(...) Handles mnuFileExit.Click
' Close the form.
Me.Close()
End Sub
COMMON DIALOG BOXES
▪ predefined standard dialog boxes e.g. opening files, saving files, and selecting
fonts and colors
▪ use common dialog control that can be found in the Toolbox window
(OpenFileDialog, SaveFileDialog, FontDialog, and ColorDialog)
▪ cannot resize
▪ location doesn’t matter, invisible when program runs (appear in component tray
atdesign time)
18
OPENFILEDIALOG CONTROL
▪ open a file either specify the filename(code) or require
user to enter the path and filename
▪ displays a standard WindowsOpen
dialog box
• ability to browse for a file to open
• naming : ofd (e.g. ofdOpenFile)
19
OPENFILEDIALOG CONTROL
▪ Displaying an Open Dialog Box
▪ displaying by calling ShowDialog()
• general form: ControlName.ShowDialog()
• E.g. : ofdOpenFile.ShowDialog()
▪ input validation (whether user has selected file or
not) using:
• Windows.Forms.DialogResult.OK
• Windows.Forms.DialogResult.Cancel
• E.g.:
20
OPENFILEDIALOG CONTROL
▪ The FilterProperty
• specifies type of files visible using Filter property
• E.g. :
*.txt (only text files are
displayed)
*.doc (only Microsoft word
files are displayed)
▪ can set the filter in Property
Window or using coding
• E.g.:
21
OPENFILEDIALOG CONTROL
▪ The InitialDirectory Property
▪ specifiy initial directory for open dialog box using
InitialDirectory property
▪ when dialog box displayed, it
shows the contents of the initial
directory
• can set in Property Window:
• using coding :
22
OPENFILEDIALOG CONTROL
▪ Using the Open Dialog Box to Open a File
▪ putting everything together in a code
• Filter, InitialDirectory, Title properties, display Open
dialog box, retrieve the filename entered, and open the
file
23
SAVEFILEDIALOG CONTROL
▪ displays a standard Windows Saveas
dialog box
• give users ability to browse their disks
and choose location to save the file
• naming : sfd (e.g. sfdSaveFile)
▪ properties same as OpenFileDialog
control
24
SAVEFILEDIALOG CONTROL
▪ Displaying and Combining All Properties
▪ displaying by calling ShowDialog()
• general form: ControlName.ShowDialog()
• E.g. : sfdSaveFile.ShowDialog()
▪ coding for input validation and combining
properties:
25
COLORDIALOG CONTROL
▪ displays a standard Windows
Color dialog box
▪ the dialog box will expands
if user clicks Define Custom
Colors
▪ found in the ToolboxWindow
• invisible at runtime, appears
only in the component tray
at design time
• naming : cd (e.g. cdColor)
26
COLORDIALOG CONTROL
▪ displays a Color dialog box using ShowDialog method
• e.g. : cdColor.ShowDialog( )
▪ hold value to be used with control properties for designate color such as
ForeColor, BackColor
• e.g. :
• this coding displays the Color dialog box and then sets the text color based
on user choices
27
COLORDIALOG CONTROL
▪ default initial color is black
▪ if want to change the initially selected
color, set in the Color property or using
coding
• Color property :
• Coding :
‘ set the initially selected color same as lblMessage ForeColor
28
FONTDIALOG CONTROL
▪ displays a standard Windows
Font dialog box
▪ Two (2) types :
• Default Font dialog box
• Font dialog box with colorchoice
▪ found in the ToolboxWindow
• invisible at runtime, appears only in
the component tray at design time
• naming : fd (e.g. fdFont)
29
FONTDIALOG CONTROL
▪ displays a Font dialog box using ShowDialog
method
• e.g. : fdFont.ShowDialog( )
▪ by default –does not allow toselect color
▪ color is controlled by ShowColor
property
• True –appears with color drop-down list
• False– default Font dialog box
30
FONTDIALOG CONTROL
▪ Hold value representing the font settings selected by user
• e.g. coding - display Font dialog box and sets lblMessage font as selected by
user:
• OR displays a Font dialog box with drop-down list of color, sets lblMessage
font and color as selected by user
31
END OF SUBTOPIC … MENUS