Programming with Microsoft Visual Basic 2017 Chapter 4

Programming with Microsoft Visual Basic 2017 Chapter 4 The Selection Structure

FOCUS ON THE CONCEPTS LESSON Concepts covered in this lesson: • • • Selection structures If…Then…Else statement Comparison operators Logical operators Summary of operators String comparisons Nested selection structures Multiple-alternative selection structures Select Case statement 2

Selection Structures (1 of 4) • Every application procedure is written using one or more of the three basic control structures: sequence, selection, and repetition structure. – Sequence structure: An invoked procedure processes its instructions in the order they appeared in the procedure. – Selection structure: In the structure, a computer makes a decision to execute the next instruction according to a condition. § The condition in a selection structure evaluates an answer of either true or false. 3

Selection Structures (2 of 4) • Single-alternative selection structure – Instructions are performed only when its condition is true. • Dual-alternative selection structure – True path § The set of instructions which is performed when its condition is true – False path § • The other set of instructions which is performed when its condition is false. The words “if” and “end if” denote a selection structure’s beginning and end, respectively. 4

Selection Structures (3 of 4) Flow chart 5

Selection Structures (4 of 4) 6
![If…Then…Else Statement • If…Then…[Else…] statement – • • Used to code single and dual-alternative If…Then…Else Statement • If…Then…[Else…] statement – • • Used to code single and dual-alternative](http://slidetodoc.com/presentation_image_h2/7f2d5d3ddeebbf27efe4677c731db36c/image-7.jpg)
If…Then…Else Statement • If…Then…[Else…] statement – • • Used to code single and dual-alternative selection structures Statement block – The set of statements contained in each path The condition in an If…Then…Else statement is a boolean expression that results in a Boolean value (True or False). 7

Comparison Operators (1 of 3) • Comparison operators – Used to compare two values – Always result in a True or False value • Rules for comparison operators: – They do not have an order of precedence. – They are evaluated from left to right. – They are evaluated after any arithmetic operators in the expression. example 8

Comparison Operators (2 of 3) 9

Comparison Operators (3 of 3) 10

Logical Operators (1 of 4) • Logical operators – Used to create compound conditions – Expressions evaluate to a Boolean value § • True or False Six logical operators in Visual Basic: – – – Not And. Also Or Or. Else Xor 11

Logical Operators (2 of 4) • Not operator – Reverses the truth-value of the condition • And operator and And. Also operator – Both operators combine two sub-conditions. – The compound condition evaluates to true only when both conditions are true. – And operator always evaluates both conditions. – And. Also performs a short-circuit evaluation, which bypasses the evaluation of the second sub-condition when the outcome can be determined without it. 12

Logical Operators (3 of 4) • Or operator or Or. Else operator – Both operators combine two sub-conditions. – The compound condition evaluates to true when either or both conditions are true. – Or. Else is more efficient than Or. § • Evaluates to true when one of the sub-conditions are true. Xor operator – The compound condition evaluates to True when only one of the sub-conditions is True. – If both sub-conditions are True or both sub-conditions are False, then the compound condition evaluates to False. 13

Logical Operators (4 of 4) 14

Summary of Operators 15

String Comparisons (1 of 3) • • Use To. Upper method to convert the string to uppercase. Use To. Lower method to convert the string to lowercase. Syntax § In the syntax, String is usually either the name of a String variable or the Text property of an object. Example • The Trim method removes any space characters from both the beginning and the end of a string. Example 16

String Comparisons (2 of 3) 17

String Comparisons (3 of 3) 18

Nested Selection Structures • • The inner selection structure is referred to as a nested selection structure. It is contained (nested) entirely within the outer selection structure. 19

Multiple-Alternative Selection Structures (1 of 2) • Multiple-alternative selection structures or extended selection structures choose from several different alternatives. 20

Multiple-Alternative Selection Structures (2 of 2) 21

Select Case Statement (1 of 2) • Select Case statement is simple and clear to code the multiple-alternative selection structure. comparison 22

Select Case Statement (2 of 2) • Specifying a Range of Values in a Case Clause 23

APPLY THE CONCEPTS LESSON After studying this lesson, you should be able to: • Add a check box to a form • Code an interface that contains check boxes • Add a radio button to a form • Code an interface that contains radio buttons • Group objects using a group box control • Professionalize your application’s interface • Professionalize your code using arithmetic assignment operators 24

A-1 Add a Check Box to a Form • • A check box provides an option that the user can either choose to select or choose not to select. Use a selection structure to determine the value in a check box’s Checked property. 25

A-2 Code an Interface That Contains Check Boxes (1 of 9) • In Seminars application, checkbox offers user three different seminar options. • When the user clicks the Calculate button, the button’s Click event procedure will calculate and display the total amount due. 26

A-2 Code an Interface That Contains Check Boxes (2 of 9) To complete the btn. Calc_Click procedure: • Open the Seminars Solution. sln file contained in the Seminars Solution-Check. Box folder. • Open the Code Editor window and locate the btn. Calc_Click procedure. • • • The procedure will use three single-alternative selection structures to determine which (if any) of the three check boxes are selected. If a selection structure’s condition evaluates to True, the instruction in its true path will add the seminar’s fee to the amount due. Enter the three selection structures. 練習 p. 152 -1~3 27

A-2 Code an Interface That Contains Check Boxes (3 of 9) 28

A-2 Code an Interface That Contains Check Boxes (4 of 9) • Save the solution and then start the application. Click the Calculate button. – No check boxes are selected, so $0 appears in the Amount due box. – Click the Health ($125) check box. § § Notice that $0 still appears in the Amount due box, which could be misleading. You will fix this problem in the next section. – Click the Calculate button. The Amount due box shows $125, which is the price of the Health seminar 29

A-2 Code an Interface That Contains Check Boxes (5 of 9) • Click the Marketing ($135) check box and click the Calculate button. • • Click the Finance ($150) check box and click the Calculate button. • • The Amount due box shows $410, the cost for all three seminars. Click the Health ($125) check box to deselect it and click the Calculate button. • • The Amount due box shows $260, cost for the Health and Marketing seminars ($125 + $135). The Amount due box shows $285, the cost for the Finance and Marketing seminars ($150 + $135). Click the Exit button. 練習 p. 152 -4~153 -8 30

A-2 Code an Interface That Contains Check Boxes (6 of 9) To code each check box’s Checked. Changed procedure: • Open the code template for the chk. Finance_Checked. Changed procedure. • Type lbl. Amount. Due. Text = String. Empty and press Enter. • Now, enter the same assignment statement in the chk. Health_Checked. Changed and chk. Marketing_Checked. Changed procedures. 練習 p. 153 -1~3 • Save the solution and then start the application. • Select all three check boxes and then click the Calculate button. • The Amount due box shows $410. 31

A-2 Code an Interface That Contains Check Boxes (7 of 9) 32

A-2 Code an Interface That Contains Check Boxes (8 of 9) • Click the Finance ($150) check box to deselect it. – The chk. Finance_Checked. Changed procedure clears the contents of the Amount due box. – Click the Calculate button. § • The Amount due box shows $260. Click the Health ($125) check box to deselect it. – The chk. Health_Checked. Changed procedure clears the contents of the Amount due box. – Click the Calculate button. § The Amount due box shows $135. 33

A-2 Code an Interface That Contains Check Boxes (9 of 9) • Click the Marketing ($135) check box to deselect it. – The chk. Marketing_Checked. Changed procedure clears the contents of the Amount due box. – Click the Calculate button. § • The Amount due box shows $0. Click the Exit button and then close the solution. 34

A-3 Add a Radio Button to a Form • Radio buttons allow selecting one choice from a group of two or more related but mutually exclusive options. • Radio button’s Checked property contains the Boolean value True when the control is selected; otherwise, False. • The automatically selected radio button is called the default radio button. 35

A-4 Code an Interface That Contains Radio Buttons (1 of 9) To begin completing this version of the application: • Open the Seminars Solution. sln file contained in the Seminars Solution-Radio. Button-If folder. • Designate a default radio button. • Click the Finance ($150) radio button and then set its Checked property to True. A colored dot appears inside the button’s circle indicating selected button. • Open the Code Editor window and locate the btn. Calc_Click procedure. • The procedure will use a multiple-alternative selection structure to determine which of the three radio buttons is selected. 36

A-4 Code an Interface That Contains Radio Buttons (2 of 9) 練習 p. 155 -1~4 37

A-4 Code an Interface That Contains Radio Buttons (3 of 9) • Save the solution and then start the application. – The Finance ($150) radio button is already selected. Click the Calculate button. § • The Amount due box shows $150. Click the Health ($125) radio button. – The computer selects the Health ($125) radio button as it deselects the Finance ($150) radio button. – This is because only one button in a group can be selected at any one time. • Click the Calculate button. – The Amount due box shows $125, which is the price of the Health seminar. 38

A-4 Code an Interface That Contains Radio Buttons (4 of 9) • Click the Marketing ($135) radio button and then click the Calculate button. – The Amount due box shows $135, which is the cost of the Marketing seminar. • Click the Exit button. 39

A-4 Code an Interface That Contains Radio Buttons (5 of 9) To code each radio button’s Checked. Changed procedure: • Enter the lbl. Amount. Due. Text = String. Empty assignment statement in each radio button’s Checked. Changed procedure. • Save the solution and then start the application. • Click the Calculate button. • The Amount due box shows $150. • Click the Health ($125) radio button. • The button’s Checked. Changed procedure clears the contents of the Amount due box. • Click the Calculate button. • The Amount due box shows $125. 40

A-4 Code an Interface That Contains Radio Buttons (6 of 9) • Click the Marketing ($135) radio button. – The button’s Checked. Changed procedure clears the contents of the Amount due box. • Click the Calculate button. – The Amount due box shows $135. • Click the Finance ($150) radio button. – The button’s Checked. Changed procedure clears the contents of the Amount due box. • Click the Calculate button. – The Amount due box shows $150. • Click the Exit button and then close the solution. 41

A-4 Code an Interface That Contains Radio Buttons (7 of 9) 練習 p. 156 -1~6 42

A-4 Code an Interface That Contains Radio Buttons (8 of 9) To use the Select Case statement in the btn. Calc_Click procedure: • Open the Seminars Solution. sln file contained in the Seminars Solution-Radio. Button-Select Case folder. • Open the Code Editor window and locate the btn. Calc_Click procedure. • Enter the Select Case statement. • Save the solution and then start and test the application. • Click the Exit button and then close the solution. 43

A-4 Code an Interface That Contains Radio Buttons (9 of 9) comparison 練習 p. 157 -1~4 44

A-5 Group Objects Using a Group Box Control (1 of 6) • A group box serves as a container for other controls and is typically used to visually separate related controls from other controls on the form. • Labeling a group box is optional, but to label it should be entered using sentence capitalization. • A group box and its controls are treated as one unit. – When you move or delete a group box, the controls inside the group box are also moved or deleted. 45

A-5 Group Objects Using a Group Box Control (2 of 6) To use a group box to group radio buttons: • Open the Radio. Button Solution. sln file contained in the Radio. Button Solution folder. • If necessary, open the designer window. The interface contains two sets of radio buttons. Final GUI • • The first set offers two choices: Coffee or Tea. The second set offers three choices: Small, Medium, or Large. • Start the application. • Click each radio button. • Notice that only one of the radio buttons on the form can be selected at any one time. • Click the Exit button. 46

A-5 Group Objects Using a Group Box Control (3 of 6) • • • Expand the Containers node in the Toolbox window. Click the Group. Box tool and then drag the tool to the upper -left corner of the form. Release the mouse button. Set the group box’s Text property to Type. Click the Coffee radio button and then Ctrl+click the Tea radio button. – Drag the selected controls into the group box. – Release the mouse button. – The interface now has two independent groups of radio buttons. Click the Type group box and then drag its middle-right sizing handle to make it smaller. 練習 p. 158 -1~6 47

A-5 Group Objects Using a Group Box Control (4 of 6) • Before starting and testing the application, you will specify the default button in each group. – – Use the Properties window to set the Coffee radio button’s Checked property to True. Also set the Small radio button’s Checked property to True. • Save the solution and then start the application. – The two default radio buttons are automatically selected in the interface. 練習 p. 158 -7~10 • Click the Tea radio button. – Selecting the Tea radio button deselects the Coffee radio button. However, it has no effect on the Small radio button, which is part of a different group. 48

A-5 Group Objects Using a Group Box Control (5 of 6) • • • Click the Medium radio button and then click the Large radio button. – Only one of the radio buttons in this group can be selected at any one time and making a selection in this group has no effect on the radio buttons in the Type group. Click the Exit button. Add another group box to the form and then modify the interface. Lock the controls on the form. Save the solution and then start and test the application. Click the Exit button and then close the solution. 練習 p. 158 -11~13 49

A-5 Group Objects Using a Group Box Control (6 of 6) 50

A-6 Professionalize Your Application’s Interface (1 of 4) • A text box’s Key. Press event occurs each time the user presses a key in the text box. • Parameter represents information that is passed to the procedure. – The e parameter’s Key. Char property determines the pressed key in a Key. Press event. – e parameter’s Handled property — cancel the key. • Refer the Backspace key on the keyboard using Visual Basic’s Control. Chars. Back constant. 51

A-6 Professionalize Your Application’s Interface (2 of 4) To code a Text Box’s Key. Press Event Procedure: 52

A-6 Professionalize Your Application’s Interface (3 of 4) To code the txt. Purchased_Key. Press procedure: • Open the Total Due Solution. sln file contained in the Total Due Solution-Key. Press folder. • Open the Code Editor window and then open the code template for the txt. Purchased_Key. Press procedure. • Enter the comment and selection structure. 練習 p. 161 -1~4 • Save the solution and then start the application. • Try typing a letter, a period, and a $ in the Number purchased box. – You will not be able to do so. 53

A-6 Professionalize Your Application’s Interface (4 of 4) • Type 12 in the Number purchased box and then click the Calculate button. • • The Total due box shows $91. 80. Click the Exit button. 54

A-7 Professionalize Your Code Using Arithmetic Assignment Operators (1 of 3) • Arithmetic assignment operators are used to abbreviate an assignment statement that contains an arithmetic operator. 55

A-7 Professionalize Your Code Using Arithmetic Assignment Operators (2 of 3) To use an assignment operator in the btn. Calc_Click procedure: • Locate the btn. Calc_Click procedure. • Modify the assignment statement that subtracts the discount from the total due. 練習 p. 162 -1~3 • Save the solution and then start the application. • Type 10 in the Number purchased box and then click the Calculate button. • The total due is $76. 50. • Click the Exit button. • Close the Code Editor window and then close the solution. 56

A-7 Professionalize Your Code Using Arithmetic Assignment Operators (3 of 3) 57

作業 • Exercises 第 2題 (課本第 172~173頁) 58

Summary (1 of 6) • • You can use the If…Then…Else statement to code single-alternative and dual-alternative selection structures. You can use the comparison operators listed to compare two values. To create a compound condition, use the logical operators. When evaluating an expression that contains arithmetic, comparison, and logical operators, – evaluate the arithmetic operators first, – followed by the comparison operators and then – the logical operators. 59

Summary (2 of 6) • • • To temporarily convert a string to either uppercase or lowercase, use the To. Upper and To. Lower methods, respectively. To remove leading and trailing spaces from a string, use the Trim method. To create a selection structure that evaluates both a primary and a secondary decision, place (nest) the secondary decision’s selection structure entirely within either the true or false path of the primary decision’s selection structure. 60

Summary (3 of 6) • • Use either If…Then…Else statements or the Select Case statement to code a multiple-alternative selection structure. To specify a range of values in a Select Case statement’s Case clause, – use the To keyword when you know both the upper and lower values in the range. • Use the Is keyword when you know only one end of the range. • The Is keyword is used in combination with one of the following comparison operators: =, <, <=, >, >=, <>. 61

Summary (4 of 6) • • • To allow the user to select any number of choices from a group of one or more independent and nonexclusive options, use the Check. Box tool to add one or more check box controls to the form. To limit the user to only one choice in a group of two or more related but mutually exclusive options, use the Radio. Button tool to add two or more radio buttons to the form. To include two groups of radio buttons on a form, at least one of the groups must be placed within a container, such as a group box. 62

Summary (5 of 6) • To determine whether a radio button or check box is selected or unselected, use the control’s Checked property. – The property will contain the Boolean value True if the control is selected; – otherwise, it will contain the Boolean value False. • To process code when the value in the Checked property of a radio button or check box changes, enter the code in the control’s Checked. Changed event procedure. 63

Summary (6 of 6) • To group controls together using a group box, use the Group. Box tool to add a group box to the form. • • To allow a text box to accept only certain keys, code the text box’s Key. Press event procedure. • • • To include an identifying label on a group box, set the group box’s Text property. The key the user pressed is stored in the e. Key. Char property. You use the e. Handled = True statement to cancel the key pressed by the user. You can abbreviate some assignment statements using the arithmetic assignment operators. 64
- Slides: 64