30
5-1 lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh

Chapter 05

Embed Size (px)

Citation preview

Page 1: Chapter 05

5-1

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

Page 2: Chapter 05

Menus, Common Dialog Boxes, Sub Procedures and

Function Procedures

Chapter 55

McGraw-Hill © 2006 The McGraw-Hill Companies, Inc. All rights reserved.

Page 3: Chapter 05

5-3

Objectives

Create menus and submenus for program controlDisplay and use the Windows common dialog boxesCreate context menus for controls and the formWrite reusable code in sub procedures and function

procedures and call the procedures from other locations

Page 4: Chapter 05

5-4

Menus

Menu BarContains menus which drop down to display list of

menu itemsCan be used in place of or in addition to buttons to

execute a procedureMenu items are controls with properties and events

Easy to create menus for a Windows form using the Visual Studio environment’s Menu Designer

Menus will look and behave like standard Windows menus

Page 5: Chapter 05

5-5

Defining Menus (1 of 2)

MenuStrip component is added to a form

MenuStrip is a container to which ToolStripMenu Items, ToolStripComboBoxes, ToolStripSeparators, and ToolStripTextBoxes can be added

Page 6: Chapter 05

5-6

Defining Menus (2 of 2)

The MenuStrip componentappears in the component traybelow the form and the MenuDesigner allows you to begintyping the text for the menuitems.

Page 7: Chapter 05

5-7

The Text Property

Holds the words that appear on the screen – like the Text property of a button or label

To conform Windows standards the first menu’s Text property should be File, with a keyboard access keyUse the ampersand (&) in the text to specify the key to

use for keyboard accessEnter 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

Page 8: Chapter 05

5-8

The Name Property

The File menu item that is added is automatically named FileToolStripMenuItem

The items are named so well that there won’t be a need to change the Name property of any menu componentIf the Text property is changed for any menu item, the

item is not automatically renamed; it will need to be renamed

Page 9: Chapter 05

5-9

The MenuStrip Items Collection

ToolStripMenu Items in the collection can be displayed, reordered, added, deleted using the Items Collection Editor

Page 10: Chapter 05

5-10

Submenus

Filled triangle to the right of the menu item indicates to the user the existence of a submenu

Create submenus by moving to the right of a menu item and typing the next item's text

Page 11: Chapter 05

5-11

Separator Bars

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-down arrow

Page 12: Chapter 05

5-12

Menu Properties

Enabled property, True/False-can be set at design or run time

Checked property, False/True-can be set at design or run timeUsed to indicate that an option is selected

Setting keyboard shortcutsSelect the menu item and In Properties window for menu

item, select the ShortcuKeyst propertyMake choice from drop-down list

Page 13: Chapter 05

5-13

Standards for Windows Menus

Follow Windows standards for applicationsInclude keyboard access keysUse standards for shortcut keys, if usedPlace the File menu at left end of menu bar and end

File menu with the Exit commandHelp, if included, is placed at right end of menu bar

File Edit View Format Help

Page 14: Chapter 05

5-14

Common Dialog Boxes

Predefined standard dialog boxes for:Specifying colors and fontsPrinting, opening, and saving

Add appropriate Common Dialog components to display the dialog boxes that are provided as part of the Windows environmentTo use a common dialog component, add the

component to the form, placing it in the component tray

Page 15: Chapter 05

5-15

Common Dialog Tools

PointerColorDialog FontBrowserDialogFontDialogOpenFileDialogSaveFileDialog

Page 16: Chapter 05

5-16

Displaying a Windows Common Dialog Box

Use ShowDialog method to display the common dialog box at run time

ShowDialog only displays the dialog

ColorDialog1.ShowDialog( )FontDialog1.ShowDialog( )

Page 17: Chapter 05

5-17

Modal versus Modeless Windows

A dialog box is said to be modal - means that it stays on top of the application and must be responded toUse the ShowDialog method to display a dialog box-it is

a window displayed modallyModeless windows do not demand that there is a

responseUse the Show method to display a modeless window

Page 18: Chapter 05

5-18

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

ExampleColor Dialog displayedUser selects color and clicks OK-the selected

color is stored in a property that can be accessedColor that is selected is stored in the Color

property and can be assigned to another object such as a control

titleLabel.BackColor = ColorDialog1.Color

Page 19: Chapter 05

5-19

Setting Initial Values

Before executing the ShowDialog method, assign the existing values of the object's properties that will be altered

When the dialog box appears, the current values will be selected

If the user presses Cancel, property settings for the objects will remain unchanged

FontDialog1.Font = .subTotalLabel.Font or ColorDialog1.Color = .BackColor

Page 20: Chapter 05

5-20

Creating Context Menus

Shortcut menus that pop up when you right-clickItems are specific to the component to which 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

Page 21: Chapter 05

5-21

Writing General Procedures

A general procedure is reusable code which can be called from multiple procedures

Useful for breaking down large sections of code into smaller units

Two typesSub Procedure performs actionsFunction performs actions AND returns a value (the

return value)

Page 22: Chapter 05

5-22

Passing Arguments to Procedures

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

Page 23: Chapter 05

5-23

Creating a New Sub Procedure

In the Editor window enclose the lines of code with 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

Page 24: Chapter 05

5-24

Sub Procedure Example

Private Sub SelectColor(incomingColor As Color)With ColorDialog1

.Color = incomingColor

.ShowDialog( )End With

End Sub

Private Sub changeTitleButtonColor_Click( )Dim originalColor As Color

originalColor = titleLabel.ForeColorSelectColor(originalColor)titleLabel.ForeColor = ColorDialog1.Color

End Sub

Sub Procedure

CallingProcedure

Page 25: Chapter 05

5-25

Passing Arguments ByVal or ByRef

ByVal -value Sends a copy of the argument’s value, original cannot

be alteredByRef -reference

Sends a reference to the memory location where the original is stored and therefore the procedure may change the argument’s original value can be altered

If not specified arguments are passed by value

Page 26: Chapter 05

5-26

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 the value must be specified

To use the Function, Call it by using it in an expression

Pass arguments ByVal or ByRef

Private Function FunctionName( ) As Datatype' Statements to execute.

End Function

Page 27: Chapter 05

5-27

Returning the Result of a Function

To return a value to the calling procedure set up a return value

The return value will be placed by VB in a variable with the SAME name as the Function's name

--OR--Use the Return statement to return the value

Page 28: Chapter 05

5-28

Function Example

Private Sub calculateButton_Click( )Dim salesDecimal As Decimal

salesDecimal = Decimal.Parse(salesTextBox.Text)commissionLabel.Text = Commission(salesDecimal.ToString("C"))

End Sub

CallingProcedure

Private Function Commission(ByVal salesAmountDecimal As Decimal) _ As Decimal

If salesAmountDecimal < 100D ThenCommission = 0D

ElseCommission = 0.15 * salesAmountDecimal

End IfEnd Function

Function

Page 29: Chapter 05

5-29

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

paymentLabel.Text = Payment(Decimal.Parse(rateTextBox.Text), _ Decimal.Parse(yearsTextBox.Text), _ Decimal.Parse(principalTextBox.Text)).ToString( )End Function

Page 30: Chapter 05

5-30

Breaking Calculations into Smaller Units

Projects with many calculations are easier to understand and write if calculations are broken into small units

Each unit should perform one program function or logic block