46
Visual Basic .NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

Embed Size (px)

Citation preview

Page 1: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

Visual Basic .NETComprehensive Concepts

and Techniques

Chapter 5

Decision Making

Page 2: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

2Chapter 5: Decision Making

Objectives

• Use the ComboBox control

• Use the If…Then…Else structure

• Use a nested If…Then…Else structure

• Use the Select Case structure

• Validate user input

Page 3: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

3Chapter 5: Decision Making

Objectives

• Use the MessageBox class

• Use the string concatenation in code

• Use relational operators in code

• Use logical operators in code

Page 4: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

4Chapter 5: Decision Making

Modifying the Automobile Loan Calculator Application

Page 5: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

5Chapter 5: Decision Making

Page 6: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

6Chapter 5: Decision Making

Page 7: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

7Chapter 5: Decision Making

Opening the Project and Modifying the User Interface• Open the Automobile Loan Calculator

program you created in Chapter 4

• Select the Form1 form in the main work area, right-click the form, and then click Lock Controls on the shortcut menu to unlock the form

• Increase the form height to 248 pixels

Page 8: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

8Chapter 5: Decision Making

Opening the Project and Modifying the User Interface• Select the Label3 control, along with

txtnMonthlyPament, btnComputePayment, and btnReset controls

• Drag the controls down four marks on the positioning grid

• Add a Label and ComboBox control from the Toolbox window and position as shown on the next slide

Page 9: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

9Chapter 5: Decision Making

Page 10: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

10Chapter 5: Decision Making

Label Control Properties

Page 11: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

11Chapter 5: Decision Making

ComboBox Control Properties

Page 12: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

12Chapter 5: Decision Making

ComboBox1 Control Properties

Page 13: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

13Chapter 5: Decision Making

Setting the Items Property

• Select the Items property in the Properties window

• Click the Collection build button

• Type the five items into the String Collection Editor, as shown on the following slide, and click the OK button

• Upon completion, lock the controls using the Format menu

Page 14: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

14Chapter 5: Decision Making

Page 15: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

15Chapter 5: Decision Making

Modifying the Comment Header and Adding a Constant

Page 16: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

16Chapter 5: Decision Making

Resetting the ComboBox Value

• The SelectedIndex property of the ComboBox control is assigned the value of the item’s index

• When no item is selected, the SelectedIndex property is set to -1

• Add the following line of code to the end of the btnReset_Click Event Procedure:

Page 17: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

17Chapter 5: Decision Making

Declaring Additional Variables

• Two additional variables are needed in the btnComputePayment_Click event procedure

• Scroll the code window until line 258 appears

• Enter line 259 as shown below:

Page 18: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

18Chapter 5: Decision Making

Decision-Making Control Structures

• Decision making is the process of determining which of one or more paths to take– If…Then…Else structure– Select Case structure

Page 19: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

19Chapter 5: Decision Making

If…Then…Else Structure

Select Case Structure

Page 20: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

20Chapter 5: Decision Making

If…Then…Else Structure for Auto Loan Calculator

Page 21: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

21Chapter 5: Decision Making

The MessageBox Class and the Show() Method• The MessageBox class provides the ability

to display a message to the user in a pop-up window

• The Show() method must be used to display a message with a title bar

• MessageBox.Show(“Please enter the customer’s credit rating in the Credit rating list box.”,”No Credit Rating”)

Page 22: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

22Chapter 5: Decision Making

The Focus() Method

• When a user selects a control on a form, the control is said to have focus

• It is often necessary to set the focus to a control during run time

• cmbCreditRating.Focus()

Page 23: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

23Chapter 5: Decision Making

If…Then…Else Statement

• Relational expression =, <, >, <=, >=, <>

• Single-line If…Then…Else statement

• Block If…Then…Else statement

• Block-level scoping

• Exit Sub Statement– Terminates execution of a procedure

immediately

Page 24: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

24Chapter 5: Decision Making

If…Then…Else Structure

• Enter the following code, starting on line 263:

• Parameter Info window

Page 25: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

25Chapter 5: Decision Making

Considering Selection Structures

Page 26: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

26Chapter 5: Decision Making

The Nested If…Then…Else Structure

Page 27: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

27Chapter 5: Decision Making

Coding a Nested If…Then…Else Structure

Page 28: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

28Chapter 5: Decision Making

Coding a Nested If…Then…Else Structure• In the code window, delete lines 240

through 251

• Starting on line 259, enter the following code

Page 29: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

29Chapter 5: Decision Making

Logical Operators

• The Not Logical Operator

• The And Logical Operator

• The Or Logical Operator

• The Xor Logical Operator

• Other Logical Operators– AndAlso– OrElse

Page 30: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

30Chapter 5: Decision Making

Combining Logical Operators

If X>Y Or T=D And H<3 Or Not Y=R Then

intCount = intCount + 1

End If• Rules of precedence

– Arithmetic operators– Relational operators– Not operators– And/AndAlso operators– Or/OrElse or Xor operators

Page 31: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

31Chapter 5: Decision Making

The Effect of Parentheses in the Evaluation of Compound Conditions

• Parentheses can be used to change the order of precedence

• Avoid ambiguity and group conditions with a desired logical operator

(C>D And S=4) Or (X<Y And T=5)

Page 32: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

32Chapter 5: Decision Making

Coding with a Logical Operator and String Expressions

Page 33: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

33Chapter 5: Decision Making

Coding with a Logical Operator and String Expressions• Concatenation (&) operatorstrErrorMessage = “Customers with this credit rating may only borrow “ & strMoney

• The plus sign (+) can be used instead of the (&) operator

Page 34: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

34Chapter 5: Decision Making

Coding with a Logical Operator and String Expressions• Enter the following code, starting on line

272

Page 35: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

35Chapter 5: Decision Making

The Select Case Structure

• Used to implement an extension of the If…Then…Else structure

Page 36: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

36Chapter 5: Decision Making

Page 37: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

37Chapter 5: Decision Making

Coding a Select Case Statement

Page 38: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

38Chapter 5: Decision Making

Coding a Select Case Statement

• Enter the following code, starting on line 284

Page 39: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

39Chapter 5: Decision Making

Modifying a Parameter in a Function Call• The function call to the Pmt function

currently accepts the dblRate variable as the interest rate parameter

• This does not take into account the customer’s credit rating

• In line 300, click between the letters l and R in the dblRate parameter in the Pmt function call and enter Adjusted to change the parameter name

Page 40: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

40Chapter 5: Decision Making

Page 41: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

41Chapter 5: Decision Making

Saving and Testing the Project

• Click the Save All button on the Standard toolbar• Click the Start button on the Visual Basic .NET

Standard toolbar• Enter 13000 for the Loan amount. Enter 10.25

for the Current interest rate in the second NumericUpDown control. Click the Compute Payment button– The No Credit Rating message box should display

Page 42: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

42Chapter 5: Decision Making

Saving and Testing the Project

• Click the OK button. Click the Credity rating box arrow and then select B – Good in the Credit rating drop-down list. Click the Compute Payment button– The resulting output should display $284.44

• Click the Close button on the Automobile Loan Calculator application window title bar

Page 43: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

43Chapter 5: Decision Making

Document the Application and Quit Visual Basic .NET• Prepare printouts of your project, as

demonstrated in Chapter 2

• Click the Visual Basic .NET Close button

Page 44: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

44Chapter 5: Decision Making

Summary

• Use the ComboBox control

• Use the If…Then…Else structure

• Use a nested If…Then…Else structure

• Use the Select Case structure

• Validate user input

Page 45: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

45Chapter 5: Decision Making

Summary

• Use the MessageBox class

• Use the string concatenation in code

• Use relational operators in code

• Use logical operators in code

Page 46: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making

Visual Basic .NETComprehensive Concepts

and Techniques

Chapter 5 Complete