Post on 04-Apr-2018
7/31/2019 visual basic note
1/29
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved.
Chapter 5
Menus, Common Dialog
Boxes, Sub Procedures,
and
Function Procedures
7/31/2019 visual basic note
2/29
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved.
5-2
Objectives
Create menus and submenus for program control. Display and use the Windows common dialog
boxes.
Create context menus for controls and the form. Write reusable code in sub procedures andfunction procedures and call the procedures from
other locations.
7/31/2019 visual basic note
3/29
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved.
5-3
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 toexecute a procedure
Menu items are controls with properties and events.
Easy to create menus for a Windows form using
the Visual Studio environments Menu Designer Menus will look and behave like standard
Windows menus.
7/31/2019 visual basic note
4/29
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved.
5-4
Defining Menus (1 of 2)
MenuStrip component isadded to a form.
MenuStrip is a container towhich ToolStripMenuItems,
ToolStripComboBoxes,
ToolStripSeparators, and
ToolStripTextBoxes can beadded.
7/31/2019 visual basic note
5/29
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved.
5-5
Defining Menus (2 of 2)
The MenuStrip componentappears in the component tray
below the form and the Menu
Designer allows you to begin
typing the text for the menu
items.
7/31/2019 visual basic note
6/29
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved.
5-6
The Text Property
Holds the words that appear on the screen like theText 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 specify the keyto use for keyboard access.
Enter and change the Text property for each menu and
menu item using the Menu Designer or make thechanges in the Text property using the Properties
window.
7/31/2019 visual basic note
7/29
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved.
5-7
The Name Property
The File menu item that is added is automaticallynamed FileToolStripMenuItem.
The items are named so well that there wont be a
need to change the Name property of any menucomponent.
If the Text property is changed for any menuitem, the item is not automatically renamed; it
will need to be renamed.
7/31/2019 visual basic note
8/29
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved.
5-8
The MenuStrip Items Collection
ToolStripMenu Items in the collection can bedisplayed, reordered, added, and deleted using the
Items Collection Editor.
7/31/2019 visual basic note
9/29
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved. 5-9
Submenus
A filled triangle to theright of the menu item
indicates the existenceof a submenu.
Create submenus bymoving to the right of a
menu item and typingthe next item's text.
7/31/2019 visual basic note
10/29
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved. 5-10
Separator Bars
Used for grouping menuitems according to their
purpose
Visually represented asa bar across the menu To create a separator
bar, add a new menu
item and click on itsdrop-down arrow.
7/31/2019 visual basic note
11/29
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved. 5-11
Menu Properties
Enabled property, True/False can be set at designor run time
Checkedproperty, False/True can be set at designor run time
Used to indicate that an option is selected Setting keyboard shortcuts
Select the menu item and in Properties window for menuitem, select the ShortcutKeys property.
Make choice from drop-down list.
7/31/2019 visual basic note
12/29
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved. 5-12
Standards for Windows Menus
Follow Windows 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 endFile menu with the Exit command. Help, if included, is placed at right end of menu bar.
File Edit View Format Help
7/31/2019 visual basic note
13/29
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved. 5-13
Common Dialog Boxes
Predefined standard dialog boxes for: Specifying colors and fonts Printing, opening, and saving
Add appropriate Common Dialog componentsto display the dialog boxes that are provided aspart of the Windows environment.
To use a common dialog component, add thecomponent to the form, placing it in the componenttray.
7/31/2019 visual basic note
14/29
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved. 5-14
Common Dialog Tools
Pointer ColorDialog FontBrowserDialog FontDialog
OpenFileDialog SaveFileDialog
7/31/2019 visual basic note
15/29
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved. 5-15
Displaying a Windows Common Dialog Box
Use ShowDialogmethod to display the commondialog box at run time.
ShowDialog only displays the dialog.
ColorDialog1.ShowDialog( )
FontDialog1.ShowDialog( )
7/31/2019 visual basic note
16/29
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved. 5-16
Modal versus Modeless Windows
A dialog box is said to be modal when it stays ontop of the application and must be responded to.
Use the ShowDialog method to display a dialog box it is a window displayed modally.
Modeless windows do not demand that there is aresponse.
Use the Show method to display a modeless window.
7/31/2019 visual basic note
17/29
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved. 5-17
Using the Information from the Dialog
Box
Code must be written to retrieve and use the choicemade by the user in the common dialog box.
Example Color Dialog is displayed. 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 propertyand can be assigned to another object such as acontrol.
titleLabel.BackColor = .ColorDialog1.Color
7/31/2019 visual basic note
18/29
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved. 5-18
Setting Initial Values
Before executing the ShowDialog method,assign the existing values of the object'sproperties that will be altered.
When the dialog box appears, the currentvalues will be selected.
If the user presses Cancel, property settings forthe objects will remain unchanged.
FontDialog1.Font = SubTotalLabel.Font or
ColorDialog1.Color = .BackColor
7/31/2019 visual basic note
19/29
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved. 5-19
Creating Context Menus
Shortcut menus that pop up when you right-click Items are specific to the component to which 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.
7/31/2019 visual basic note
20/29
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved. 5-20
Writing General Procedures
A general procedure is reusable code that can becalled from multiple procedures.
Useful for breaking down large sections of code into
smaller units Two types Sub Procedure performs actions. Function performs actions AND returns a value (the return
value).
7/31/2019 visual basic note
21/29
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved. 5-21
Passing Arguments to Procedures
Declare variable as local and pass to any calledprocedures (can be module level, but it makes thevariable visible to all other procedures)
If a sub procedure names an argument, any call tothe procedure must supply the argument.
Name of the argument does not have to be the samein both locations.
Number of arguments, sequence, and data typemust match.
7/31/2019 visual basic note
22/29
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved. 5-22
Creating a New Sub Procedure
In the Editor window, enclose the lines of codewith a set of Sub and End Sub statements.
To use the Sub Procedure, call it from anotherprocedure.
Code in a Sub Procedure cannot be executedunless called from another procedure.
Private Sub ProcedureName( )
' Statements in the procedure.
End Sub
7/31/2019 visual basic note
23/29
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved. 5-23
Sub Procedure Example
Private Sub SelectColor(incomingColor As Color)With ColorDialog1
.Color = incomingColor
.ShowDialog( )
End With
End Sub
Private Sub ChangeTitleButton_Click( )
Dim originalColor As Color
originalColor = TitleLabel.ForeColor
SelectColor(originalColor)TitleLabel.ForeColor = ColorDialog1.Color
End Sub
Sub Procedure
Calling
Procedure
7/31/2019 visual basic note
24/29
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved. 5-24
Passing Arguments ByVal or ByRef
ByVal valueSends a copy of the arguments value; original
cannot be altered.
ByRefreferenceSends a reference to the memory location where
the original is stored and therefore the procedure
may change the arguments value; original can bealtered
If not specified, arguments are passed by value.
7/31/2019 visual basic note
25/29
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved. 5-25
Writing Function Procedures
In the Editor window, enclose the linesof code with Private Function( ) and End Function
statements.
Since the procedure returns a value, a data type for thevalue must be specified.
To use the Function, Call it by using it in an expression. Pass arguments ByVal or ByRef.
Private Function ProcedureName( ) As Datatype
' Statements to execute.
End Function
7/31/2019 visual basic note
26/29
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved. 5-26
Returning the Result of a Function
To return a value to the calling procedure, set up areturn value.
The return value will be placed by VB in a variablewith the SAME name as the Function's name.
--OR--
Use the Return statement to return the value.
7/31/2019 visual basic note
27/29
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved. 5-27
Function Example
Private Sub CalculateButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles CalculateButton.Click
Dim SalesDecimal As DecimalSalesDecimal = Decimal.Parse(SalesTextBox.Text)
CommissionLabel.Text = Commission(SalesDecimal).ToString( "C")
End With
End Sub
Calling
Procedure
Private Function Commission(ByVal SalesAmountDecimal As Decimal) _As Decimal
If SalesAmountDecimal < 1000D Then
Commission = 0D
ElseIf SalesAmountDecimal
7/31/2019 visual basic note
28/29
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved. 5-28
Functions with Multiple Arguments
Functions can receive one or more arguments (values). Sequence and data type of arguments in Call must exactly
match arguments in function header.
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((AmountDecimal * RatePerMonthDecimal)
/ ((1 - (1 / (1 + RatePerMonthDecimal) ^ (TimeDecimal * 12D)))))
End Function
7/31/2019 visual basic note
29/29
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved. 5-29
Breaking Calculations into Smaller Units
Projects with many calculations are easierto understand and write if calculations are
broken into small units.
Each unit should perform one programfunction or logic block.