Presentation is loading. Please wait.

Presentation is loading. Please wait.

McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 4 Decisions and Conditions.

Similar presentations


Presentation on theme: "McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 4 Decisions and Conditions."— Presentation transcript:

1 McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 4 Decisions and Conditions

2 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 2 McGraw-Hill/Irwin Decisions and Conditions Block If statements Nested If statements Evaluating Conditions and Relational Operators Testing Values with option buttons & check boxes Validating input numeric fields Creating Message boxes Calling event procedures Debugging with breakpoints & stepping execution

3 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 3 McGraw-Hill/Irwin Decision Making Alternative courses of action are required in a program The IF statement provides one of these structures There are two types of IF statements –Single-line IF statement –Multi-line IF statement

4 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 4 McGraw-Hill/Irwin Comparison Operators SymbolMeaning >Greater than <Less than =Equal to <=Less than or equal to >=Greater than or equal to <>Not equal to

5 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 5 McGraw-Hill/Irwin IF Statement Structure (1) Sleepy? Go to bed True False Form: If condition Then statement(s) End If

6 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 6 McGraw-Hill/Irwin IF Statement Structure (2) Is SAT > 600? Admit Do Not Admit TrueFalse Form: If condition Then t-statement(s) Else f-statement(s) End If

7 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 7 McGraw-Hill/Irwin IF Statement Structure (3) Form: If condition-1 Then If condition-2 Then t-statement-2 Else f-statement-2 End If Else f-statement-1 End If False Is SAT > 600? Do Not Admit True Is GPA > 3.75? Admit True Consider False

8 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 8 McGraw-Hill/Irwin Conditions Comparing Numeric Variables & Constants Comparing Strings Comparing text property of text boxes Uppercase and lowercase character comparisons Compound conditions

9 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 9 McGraw-Hill/Irwin If & opt. buttons & check boxes If chkFlag = True Then imgFlag.Visible = True If optDisplayForm Then frmSecond.Show

10 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 10 McGraw-Hill/Irwin Compound Conditions If a1 a4 and a5 < a6 Then And has precedence over Or All comparison operators have precedence over all logical operators Use parentheses to alter the order of evaluation

11 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 11 McGraw-Hill/Irwin "If" statement and option buttons

12 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 12 McGraw-Hill/Irwin Displaying Messages in Message boxes Special window displaying message to user Form: MsgBox “message” [,buttons][, “t.b. caption”] Example: MsgBox “Numeric ID only”, vbOkOnly, “Error”

13 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 13 McGraw-Hill/Irwin Displaying a Message String Use & to concatenate strings (“Concatenate” means join end to end) The VB intrinsic constant vbCRLF creates a new line in a string MsgBox stMessage, vbOKOnly, stTitle

14 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 14 McGraw-Hill/Irwin Message box return values ConstantValueDescription vbOK1OK button pressed. vbCancel2Cancel button pressed. vbAbort3Abort button pressed. vbRetry4Retry button pressed. vbIgnore5Ignore button pressed. vbYes6Yes button pressed. vbNo7No button pressed.

15 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 15 McGraw-Hill/Irwin Input Validation Checking a data type: IsNumeric & IsDate IsNumeric checks & returns true or false If IsNumeric(txtQty.Text) Then lblDue.Caption = curPrice + Val(txtQty) Validating value ranges If Val(txtHours.Text) > 10 And _ Val(txtHours.Text) <= 80 Then...

16 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 16 McGraw-Hill/Irwin Data Validation IsDate returns true or false depending on whether or not a value is a date If IsDate(txtData) Then … the VarType function return a number that corresponds to the data type stored in a variant. If VarType(varValue) = 0 Then...

17 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 17 McGraw-Hill/Irwin Using LostFocus to Validate LostFocus is a way to validate check boxes before allowing user to go on. But is it a good way? Consider the code If txtUserName.Text = "" Then txtUserName.SetFocus Problem: some object gets focus and you cannot have allow both objects to be tested using the LostFocus event.

18 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 18 McGraw-Hill/Irwin Calling Event Procedures An event procedure is a subprocedure that reacts to a specific event such as a button click. You can call any given event procedure from multiple locations, as long as the procedure is in the same form or is public Example: Call cmdCalculate_Click Suffix is event, prefix is object name

19 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 19 McGraw-Hill/Irwin Hands on Programming Example

20 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 20 McGraw-Hill/Irwin Debugging VB Projects Debug projects by setting code breakpoints –Run the project, –Step through the code window at break time by pressing F8, –Point to variables above executed code lines to view their current values (see program shot in Notes portion of this slide)

21 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 21 McGraw-Hill/Irwin Debugging continued You can choose "Step into" or "Step over" Step into traces all code execution, including traversing down into subprocedures Step over stays with current subprocedure only, not showing any called routines' code


Download ppt "McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 4 Decisions and Conditions."

Similar presentations


Ads by Google