7/31/2014
Objectives
Chapter 5
Menus, Common
Dialog Boxes, Sub
Procedures, and
Function
Procedures
McGraw-Hill
5-2
Copyr ight 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
Defining Menus (1 of 2)
Menus
Menu Bar
Contains menus which drop down to display list of
menu items
Can be used in place of or in addition to buttons
Create menus and submenus for program control.
Display and use the Window s common dialog
boxes.
Create context menus for controls and the form.
Write reusable code in sub procedures and
function procedures and call the procedures from
other locations.
to
execute a procedure
Menu items are controls with properties and events.
Easy to create menus for a Window s form using
the Visual Studio environments Menu Designer
Menus w ill look and behave like standard
Window s menus.
MenuStrip component is
added to a form.
MenuStrip is a container to
w hich ToolStripMenuItems,
ToolStripComboBoxes,
ToolStripSeparators, and
ToolStripTextBoxes can be
added.
5-3
5-4
The Text Property
Defining Menus (2 of 2)
Holds the words that appear on the screen like the
Text property of a button or label
To conform to Windows standards, the first menus Text
property should be File, with a keyboard access key.
Use the ampersand (&) in the text to specif y the key
The MenuStrip component
appears in the component tray
below the f orm and the Menu
Designer allows y ou to begin
ty ping the text for the menu
items.
5-5
to use f or key board access.
Enter and change the Text property for each menu and
menu item using the Menu Designer or make the
changes in the Text property using the Properties
window.
5-6
7/31/2014
The Name Property
The MenuStrip Items Collection
The File menu item that is added is automatically
named FileToolStripMenuItem.
The items are named so w ell that there w ont be a
need to change the Name property of any menu
component.
If the Text property is changed for any menu
item, the item is not automatically renamed; it
w ill need to be renamed.
ToolStripMenu Items in the collection can be
displayed, reordered, added, and deleted using the
Items Collection Editor.
5-7
5-8
Separator Bars
Submenus
A filled triangle to the
right of the menu item
indicates the existence
of a submenu.
Create submenus by
moving to the right of a
menu item and typing
the next item's text.
Used for grouping menu
items according to their
purpose
Visually represented as
a bar across the menu
To create a separator
bar, add a new menu
item and click on its
drop-dow n arrow.
5-9
5-10
Menu Properties
Standards for Windows Menus
Enabled property, True/False can be set at design
or run time
Checked property, False/True can be set at design
or run time
Used to indicate that an option is selected
Setting keyboard shortcuts
Select the menu item and in Properties window f or menu
item, select the ShortcutKeys property.
Make choice f rom drop-down list.
5-11
Follow Window s standards for applications.
Include keyboard access keys.
Use standards for shortcut keys, if used.
Place the File menu at left end of menu bar and end
File menu w ith the Exit command.
Help, if included, is placed at right end of menu bar.
File Edit View Format Help
5-12
7/31/2014
Common Dialog Tools
Common Dialog Boxes
Predefined standard dialog boxes for:
Specif ying colors and fonts
Printing, opening, and sav ing
Add appropriate Common Dialog components
to display the dialog boxes that are provided as
part of the Window s environment.
To use a common dialog component, add the
component to the form, placing it in the component
tray .
Pointer
ColorDialog
FontBrow serDialog
FontDialog
OpenFileDialog
SaveFileDialog
5-13
5-14
Modal versus Modeless Windows
Displaying a Windows Common Dialog Box
Use Show Dialog method to display the common
dialog box at run time.
Show Dialog only displays the dialog.
A dialog box is said to be modal w hen it stays on
top of the application and must be responded to.
Use the ShowDialog method to display a dialog box
ColorDialog1.ShowDialog( )
FontDialog1.ShowDialog( )
it is a window display ed modally.
Modeless w indows do not demand that there is a
response.
Use the Show method to display a modeless window.
A modeless window can be ignored by the user.
5-15
5-16
Setting Initial Values
Using the Information from the Dialog Box
Code must be written to retrieve and use the choice
made by the user in the common dialog box.
Example
Color Dialog is display ed.
User selects color and clicks OK the selected
color is stored in a property that can be accessed.
Color that is selected is stored in the Color property
and can be assigned to another object such as a
control.
Before executing the Show Dialog method,
assign the existing values of the object's
properties that w ill be altered.
When the dialog box appears, the current
values w ill be selected.
If the user presses Cancel, property settings for
the objects w ill remain unchanged.
FontDialog1.Font = SubTotalLabel.Font or
ColorDialog1.Color = .BackColor
T itleLabel.BackColor = .ColorDialog1.Color
5-17
5-18
7/31/2014
Creating Context Menus
Writing General Procedures
Shortcut menus that pop up w hen you right-click
Items are specific to the component to w hich the
user is pointing, reflecting options available for
that component or situation.
A ContextMenuStrip component is added and
appears in the component tray below the form.
A context menu does not have a top-level menu,
only menu items.
Application can have multiple context menus.
A general procedure is reusable code that can be
called from multiple procedures.
Useful for breaking dow n large sections of code into
smaller units
Tw o types
A Sub Procedure perf orms actions.
A Function Procedure perf orms actions AND returns a
v alue (the return value).
5-19
5-20
Passing Arguments to Procedures
Creating a New Sub Procedure
Declare variable as local and pass to any called
procedures (can be module level, but it makes the
variable visible to all other procedures)
If a sub procedure names an argument, any call to
the procedure must supply the argument.
Name of the argument does not have to be the same
in both locations.
Number of arguments, sequence, and data type
must match.
In the Editor w indow, enclose the lines of code
w ith a set of Sub and End Sub statements.
To use the Sub Procedure, call it from another
procedure.
Code in a Sub Procedure cannot be executed
unless called from another procedure.
Private Sub ProcedureName( )
' Statements in the procedure.
End Sub
5-21
5-22
Passing Arguments ByVal or ByRef
Sub Procedure Example
Private Sub SelectColor(incomingColor As Color)
With ColorDialog1
.Color = incomingColor
.ShowDialog( )
End With
End Sub
Sub Procedure
Priv ate Sub ChangeTitleButton_Click( )
Dim OriginalColor As Color
OriginalColor = TitleLabel.ForeColor
SelectColor(originalColor)
TitleLabel.ForeColor = ColorDialog1.Color
End Sub
Calling
Procedure
5-23
ByVal value
Sends a copy of the arguments value; original
cannot be altered.
ByRef reference
Sends a reference to the memory location w here
the original is stored and therefore the procedure
may change the arguments value; original can be
altered
If not specified, arguments are passed by value.
5-24
7/31/2014
Returning the Result of a Function
Writing Function Procedures
In the Editor window, enclose the lines
of code with Priv ate Function( ) and End Function
statements.
Since the procedure returns a v alue, a data type for the
v alue must be specified.
To use the Function, Call it by using it in an expression.
Pass arguments ByVal or ByRef.
To return a value to the calling procedure, set up a
return value.
The return value w ill be placed by VB in a variable
w ith the SAME name as the Function's name.
--OR-Use the Return statement to return the value.
Private Function ProcedureName( ) As Datatype
' Statements to execute.
End Function
5-25
5-26
Functions with Multiple Arguments
Function Example
Functions can receive one or more arguments (values).
Sequence and data ty pe of arguments in Call must exactly
match arguments in function header.
Private Function Commission(ByVal SalesAmountDecima l As Decimal) _
As Decimal
If SalesAmountDecimal < 1000D Then
Commission = 0D
ElseIf SalesAmountDecima l <= 2000D Then
Commission = 0.15D* SalesAmountDecimal
Else
Function
Commission = 0.2D * SalesAmountDecimal
End If
End Function
Private Function Payment(ByVal RateDecimal As Decimal, ByVal
TimeDecimal As Decimal, _
ByVal AmountDecimal As Decimal) As Decimal
Dim RatePerMonthDecimal As Decimal
RatePerMonthDecimal = RateDecimal / 12D
' Calculate and set the return value of the function.
Payment = Convert.ToDecimal((AmountDeci mal * RatePerMonthDecimal)
/ ((1 - (1 / (1 + RatePerMonthDecimal) ^ (TimeDecimal * 12D)))))
End Function
Private Sub CalculateButton_Clic k(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles CalculateButton.Click
Calling
Dim SalesDecimal As Decimal
Procedure
SalesDecimal = Decimal.Parse(SalesTextBox.Te xt)
CommissionLabel.Text = Commission(SalesDec ima l).ToStri ng(" C")
End With
End Sub
5-27
5-28
Breaking Calculations into Smaller Units
Projects w ith many calculations are easier to
understand and w rite if calculations are broken into
small units.
Each unit should perform one program function or
logic block.
5-29