0% found this document useful (0 votes)
27 views26 pages

Menus

The document discusses menus, menu strips, and how to create and customize menus in applications. Menus are used instead of or in addition to buttons and allow adding menu items, submenus, separators, and other controls. The JSON format summarizes the key topics covered.

Uploaded by

purityn613
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views26 pages

Menus

The document discusses menus, menu strips, and how to create and customize menus in applications. Menus are used instead of or in addition to buttons and allow adding menu items, submenus, separators, and other controls. The JSON format summarizes the key topics covered.

Uploaded by

purityn613
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Menus

•Menus are used in almost all application programs,


often instead of buttons because they take less space
on the form layout. They may also be used in addition
to existing buttons.
• Menus are built through use of a menu bar – these
have menus that drop down to display menu items.
Menu items have properties and methods and events,
such as the Name and Text properties and the Click
event.
Menu strips
• MenuStrips are container controls – you can
add ToolStripMenuItems,
ToolStripComboBoxes,
ToolStripSeparators, and
ToolStripTextBoxes – menu design is very
flexible.
Creating Menus

• Begin by making room on the form for a


MenuStrip control – highlight all existing
controls on the form and drag them down to
make room for a menu strip that will display
horizontally across the top of the form.
Menu strip component
• Add a MenuStrip component to the form from
the Toolbox a control named MenuStrip1 will
display in the system component tray at the
bottom of the design screen When the MenuStrip
control is selected, it displays the MenuStrip
docked (positioned) at the top of the form.
• This is called the Menu Designer – clicking
anywhere on the form closes the Menu Designer –
clicking the MenuStrip control again opens the
Menu Designer.
Menu Design
• Each time you type a menu entry such as &File where
the MenuStrip says "Type Here", a new "Type Here"
opens up both below and to the right of the current
menu item.
• · ToolStripMenuItems under the
FileToolStripMenuItem – these are drop-down menus.
• · Drop-down menus can be rearranged by
dragging/dropping them until the menus are in the
order desired. You can right-click a menu to delete it.
• · Submenus – you can create submenus to any
depth desired.
• · A menu item with a "..." such as "Color..."
indicates that the menu will open a dialog box window.
properties
• Text Property
• As you type the words for a menu (called a ToolStripMenuItem), the typed value
is saved to the menu item's Text property.
• · The standard for ordering top-level menus is: File, Edit, and View (if used)
followed by any specialty top-level menu items. The last two menus are always
Window and Help if they are used.
• · Menus often have hot keys – typing an ampersand (&) before a letter causes
it to be underlined and to be a hot key for keyboard access by the Alt+letter
method. Example: &File.
• Name Property
• VB automatically assigns useful names to the ToolStripMenuItems. Example:
• · The File menu item will be named FileToolStripMenuItem.
• · The Exit menu item will be named ExitToolStripMenuItem.
• If you later change the Text property of a ToolStripMenuItem, VB does not
change the name – if you want to rename the menu item, you must change the
Name property.
MenuStrip Items Collection
• Menu items (ToolStripMenuItems) added through the Menu
Designer are added to an Items collection property of the
MenuStrip control. There are 3 ways to access the Items
Collection Editor that is used to set menu properties:
• 1) Click the MenuStrip (not one of the ToolStripMenuItems)
and Items collection property to use the Items Collection
Editor.
• 2) Right-click the MenuStrip – select Edit Items from the
context menu.
• 3) Click the MenuStrip's smart-tag arrow shown at the end
of the strip and select Edit Items.

Items Collection Editor
• Items Collection Editor – individual
ToolStripMenuItems are shown as Members
of the DropDownItems collection in the left
window pane – the right window pane shows
properties of the selected menu item to be
set.
Separator Bars and Other Features

• A Separator Bar is a line drawn across a menu


to separate items by purpose. Create them by:
• Access a "Type Here" and click the drop-down
arrow.
• Select Separator from the drop-down list.
• Notice that you can also add the drop-down
combo box and text box controls to a menu.
You can also add a menu to other objects on a
form, such as a group box control.
Exercise
• Create the menus shown below for a new project.
· &File
• &Color...
• &Font...
• Add a Separator bar here.
• E&xit
• · &Actions
• &Compute
• &Reset Form
• &Total Salary Information
• · &Help
• &About
Other Menu Properties & Information

• Enabled: All menus default the Enabled property to


True. If a menu is disabled, it is grayed out at run
time.
• You can set the Enabled property at design time with
the Property window or in code as shown here:
• TotalSalaryInformationToolStripMenuItem.Enabled =
False
• Checked: Set Checked = True to cause a
check mark to display beside a menu items –
the item is checked. The Checked property
defaults to False.
• You can set the Checked property at design
time or in code as shown here:
• TotalSalaryInformationToolStripMenuItem.Ch
ecked = T
ShortcutKeys and ShowShortcutKeys
• Shortcut keys are used to directly access a menu without
going through the hot keys or without using the mouse to
display the dropdown menu options. Select the menu item,
then select the Properties window – access the ShortcutKeys
property and use the dropdown to set a menu's shortcut key
combination eg Ctrl+T for the
TotalSalaryInformationToolStripMenuItem.
• The ShowShortcutKeys property must be True.
• BackColor and Font: Change the MenuStrip1 control’s
BackColor and Font properties to match those in use for the
form in general. The BackColor should be the sandy color
used for the rest of the form. The Font should be 10-point
and Bold.
Coding a Menu Click Event
• The primary event coded for a menu is the
Click event – code it as you would a button's
click event sub procedure. Double-click the
menu item to access the coding window.
• The example code below is for the Help-About
menu – a message box is used to display
information about the application.
Click event
•Private Sub AboutToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
AboutToolStripMenuItem.Click
• 'Display about message box
• Dim MessageString As String = "Programmed by john
Maingi" & ControlChars.NewLine & "Today's Date/Time: " &
Date.Now.ToString
• Dim TitleString As String = "About the Payroll
Application"
• 'Display output message
• MessageBox.Show(MessageString, TitleString ,
MessageBoxButtons.OK, MessageBoxIcon.Information)
• End Sub
• The message is stored to MessageString, a
string variable by use of concatenation with
the ControlChars.NewLine enumeration.
• · The Date.Now.ToString method returns
the current date and time for display.
• · The title information is stored to the
TitleString string variable.
Calling an Event Sub Procedure from a Menu Click Event

• When a menu option and a button control are provided to


perform the same process, such as the Action menu,
Compute option and the Compute Button, you want these
two options/controls to work exactly the same way. There are
several options for coding the Click event for this menu.
• 1) UNACCEPTABLE OPTION – Copy the code from the
Compute button Click event sub procedure – this option is
NOT good because you will have two programming code
modules to be maintained if the code ever needs to be
modified – this can lead to code that doesn't execute the
same for each control (button and menu).
• 2) ACCEPTABLE OPTION – Call the Compute button Click
event sub procedure – this is the best approach.
• There are two satisfactory approaches for
calling another Click event sub procedure.
• 1) Modify the Handles clause of the original
sub procedure
• 2) Use the PerformClick method.
Handles Clause:
• This example shows how to code a Compute
button's Click event sub procedure so that it
handles two different events. The addition to
the Handles clause is shown highlighted with
yellow.
• · This sub procedure will now execute for
both of these Click events.
• Private Sub
ComputeButton_Click(ByVal
sender As System.Object, ByVal
e As System.EventArgs) Handles
ComputeButton.Click,
ComputeToolStripMenuItem.Click
PerformClick Method
• This example shows how to code a Click event for a
control, such as a menu, so that it calls the Click event
for another control, such as a button.
• Private Sub ExitToolStripMenuItem_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles
ExitToolStripMenuItem.Click
• 'Call the ResetForm button's Click event
• ExitButton.PerformClick()
• End Sub
General Procedures
When a programming module become quite large, and
it makes sense from a logical design perspective to
divide the module into two or more separate
procedures.
• You can store the code to a separate procedure, and
then call the code from multiple button and menu
control click events.
• · You can also develop a procedure that you
"reuse" by calling it from either a menu button that is
clicked. This is a third way to avoid duplicating
(copying) code in a program, along with the
PerformClick method and the alteration of the Handles
clause.
Types of general procedures:

• Sub procedures that perform actions, but are not associated with any
specific event such as a click event. One use of sub procedures is to
set property values for a series of objects. Another use is to perform
an action such as shutting down an application by use of a standard
shutdown process.
• · Function procedures (also called user-defined functions [UDFs] or
simply functions) that perform actions, are not associated with any
specific event, and that also return a value. You use functions to
calculate values that are returned to the calling procedure.
• Both sub and function procedures are treated as methods (members)
of the form and are listed with the namespace for the form.
• Neither sub nor function procedures are associated with an event –
they are separate procedures.
Creating a Sub Procedure

• Type the following line and press the Enter


key (give the sub procedure some unique,
meaningful name):
• Private Sub ProcedureName
• · This causes VB to add parentheses and
the End Sub statement. You can then add
your code inside the procedure.
• ·
Assignment 3
• Create a function that can be used to calculate
the area of a circle. Create a program that can
be used to call the function you have created.

You might also like