Presentation is loading. Please wait.

Presentation is loading. Please wait.

Visual Basic 2010 How to Program © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

Similar presentations


Presentation on theme: "Visual Basic 2010 How to Program © 1992-2011 by Pearson Education, Inc. All Rights Reserved."— Presentation transcript:

1 Visual Basic 2010 How to Program © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

2  In Chapter 2, we showed how to create a simple GUI application using visual programming.  We defined the application’s appearance by dragging and dropping GUI controls onto a Form and setting properties in design mode—without writing any program code.  The application you created in Section 2.6 displayed text and an image but did not perform any other actions.  Visual Basic programmers use a combination of visual programming and conventional programming techniques. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

3  In this section, you’ll modify the Label ’s text programmatically, causing the text displayed on the Form to change when you execute the program.  You’ll also learn how to write code that performs an action when the Form loads—that is, when the program executes and displays the Form.  We begin by considering the program’s code and sample execution (Fig. 3.2). © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

4

5

6  Visual Basic Is Not Case Sensitive ◦ Visual Basic keywords and identifiers are not case sensitive. ◦ Uppercase and lowercase letters are considered to be identical, so asimpleprogram and ASimpleProgram are interpreted as the same identifier. ◦ Although keywords appear to be case sensitive, they’re not. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

7  The Form’s Load Event and Method ASimpleProgram_Load ◦ GUIs are event driven. ◦ When the user interacts with a GUI component, the interaction—known as an event—causes the program to perform a task by “calling” a method. ◦ Common events (user interactions) include clicking a Button, selecting an item from a menu, closing a window and moving the mouse. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

8  All GUI controls, including Form s, have events associated with them.  A method that performs a task in response to an event is called an event handler, and the process of responding to events is known as event handling.  Most of a GUI application’s functionality executes based on events.  Event handling methods are called automatically. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

9  A common event for a Form is its Load event, which occurs just before a Form is displayed on the screen— typically as a result of executing the program.  Lines 5–9 define the method ASimple-Program_Load as the Form ’s Load event handler.  When this event is raised (that is, the event occurs), method ASimple-Program_Load executes to perform its task—changing the text in the Label. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

10  At the end of line 6, the clause  Handles MyBase.Load indicates that method ASimple-Program_Load is the one that will be called to handle the Form ’s Load event.  The IDE automatically inserts this clause for you when you create the event handler. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

11  Defining a Method ◦ The keyword Sub (line 5) begins the method declaration (the code that will be executed by this method). ◦ The keywords End Sub (line 9) close the method declaration. ◦ The body of the method declaration appears between the keywords Sub and End Sub. ◦ The keyword Sub is short for “subroutine”—an early term for method. ◦ Methods are also sometimes called procedures. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

12  Modifying a Label ’s Text with Code ◦ Line 8 in Fig. 3.2 does the “real work” of the program, displaying the phrase Visual Basic is fun!. ◦ Line 8 instructs the computer to perform an action—namely, to change the text on the Label to the characters contained between the double quotation marks. ◦ These characters and the surrounding double quotes are called strings, character strings or string literals. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

13  When this line executes, it changes the Label ’s Text property to the message Visual Basic is fun!.  This updates the text on the Form (Fig. 3.2).  The statement uses the assignment operator (=) to give the Text property a new value.  The statement is read as, “ Label1.Text gets the value "Visual Basic is fun!".” © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

14  Viewing the Form ’s Code ◦ There are several ways to view the code for ASimpleProgram :  Right click the Form and select View Code.  Right click the ASimpleForm.vb file in the Solution Explorer and select View Code.  Press the F7 key.  Select Code from the View menu.  Figure 3.3 shows ASimpleProgram.vb ’s initial contents.  The editor window contains some Visual Basic code generated by the IDE. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

15  We’ll now build an Addition program (Fig. 3.8) that allows the user to enter two integers (whole numbers) then click an Add Button to calculate their sum and display the result. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

16

17

18  The Addition Program’s GUI ◦ The GUI for this program consists of three Label s, two TextBox es and a Button. ◦ The application user cannot directly modify the text on a Label, but as in Fig. 3.2, a Label ’s text can be changed programmatically by modifying the Label ’s Text property. ◦ The Label s Enter first integer: and Enter second integer: are called prompts—they direct the user to take action. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

19  A Button is a control that the user clicks to trigger a specific action in the program.  When the user clicks the Add Button, this program reads the values typed by the user in the two TextBoxe s, adds the values and displays the result.  The text on a Button is specified using its Text property. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

20  Variable Declarations and Naming ◦ Lines 8–10 are declarations, which begin with keyword Dim. ◦ The words number1, number2 and total are identifiers for variables—locations in the computer’s memory where values can be stored for use by a program. ◦ Types defined as part of the Visual Basic language, such as Integer, are known as primitive types and their type names are keywords (Fig 3.9). © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

21

22  What if the User Doesn’t Enter an Integer? ◦ For this program, if the user types a noninteger value, such as " hello," a runtime error (an error that has its effect at execution time) occurs. ◦ The message displayed in Fig. 3.10 appears when you run the application using Debug > Start Debugging (or press F5). ◦ In this case, you can terminate the program by selecting Debug > Stop Debugging or typing Ctrl + Alt + Break. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

23 ◦ Displaying the Result in resultLabel  Line 15 displays the total of the two values by assigning a new value to the resultLabel ’s Text property.  The expression  "The sum is " & total  uses the string concatenation operator, &, to combine the string literal "The sum is " and the value of Integer variable total (the sum calculated in line 14).  The string concatenation operator is a binary operator that joins two strings together, resulting in a new, longer string.  If one of the operands is a number, the program automatically creates a string representation of the number. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

24

25 ◦ Creating the resultLabel and Resizing the Form  Drag a Label onto the Form.  Set its Name property to resultLabel and delete the value of the Text property so that the Label is blank when the application begins executing—remember- that we set this text programmatically when the user clicks the Add button to add the numbers.  Set the Label ’s AutoSize property to False so that you’ll be able to size the Label.  Set the Label ’s BorderStyle property to Fixed3D to give the Label a three-dimensional appearance.  We use this style to highlight the fact that the Label displays the program’s results. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

26

27  Division Operators ◦ Visual Basic has separate operators for integer division (the backslash, \ ) and floating-point division (the forward slash, / ). ◦ Integer division takes two Integer operands and yields an Integer result; for example, the expression 7 \ 4 evaluates to 1, and the expression 17 \ 5 evaluates to 3. ◦ Any fractional part in an Integer division result simply is truncated- (that is, discarded)—no rounding occurs. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

28  When floating-point numbers (that is, numbers that contain a decimal point, such as 2.3456 and –845.7840) are used with the integer division operator, the numbers are first rounded to the nearest whole number, then divided.  This means that, although 7.1 \ 4 evaluates to 1 as expected, the statement 7.7 \ 4 evaluates to 2, because 7.7 is rounded to 8 before the division occurs.  To divide floating-point numbers without rounding the operands (which is normally what you want to do), use the floating-point division operator. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

29  The If…Then statement allows a program to make a decision based on the truth or falsity of some expression.  The expression in an If … Then statement is called a condition.  If the condition is met (that is, the condition is true), the statement in the If … Then statement’s body executes.  If the condition is not met (that is, the condition is false), the body statement does not execute. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

30

31

32

33

34

35  Getting the Values Entered By the User ◦ Lines 9–10 declare the variables that are used in the compareButton_Click event handler. ◦ The comment that precedes the declarations indicates the purpose of the variables in the program. ◦ Lines 12–13 get the numbers that the user entered and assign the values to Integer variables number1 and number2, respectively. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

36  The If … Then Statement ◦ The If … Then statement in lines 15–17 compares the values of the variables number1 and number2 for equality. ◦ If the values are equal, the statement in line 16 outputs a string indicating that the two numbers are equal. ◦ The keywords End If (line 17) end the body of the If … Then statement. ◦ Assignment and the equality operator both use the = symbol. ◦ When a condition is expected (such as after the If keyword in an If … Then statement), the = is used as an equality operator. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

37  Displaying Text in a Multiline TextBox ◦ In this program, we display several lines of text in a TextBox. ◦ To enable this functionality, we set the TextBox ’s MultiLine property to True in the Properties window. ◦ We also use the TextBox ’s AppendText method, which enables us to add more text to what is already displayed in a TextBox. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

38  The statement in line 16 is known as a method call because it “calls” a method (that is, method AppendText of class TextBox ) to ask the method to perform its task.  Sometimes you give a method values—known as arguments—that the method uses while performing its task.  In line 16 of Fig. 3.27, the expression number1 & " = " & number2 in parentheses is the argument to method AppendText. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

39  In line 16, if number1 contains the value 333 and number2 contains the value 333, the expression inside the parentheses following AppendText evaluates as follows: number1 is converted to a string and concatenated with the string " = ", then number2 is converted to a string and concatenated with the resulting string from the first concatenation.  At this point, the string "333 = 333" is appended to the TextBox ’s Text property by method AppendText. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

40  As the program proceeds through the remaining If … Then statements (lines 19–39), additional strings are appended by the resultTextBox.AppendText statements.  For example, when given the value 333 for number1 and number2, the conditions in lines 32 and 37 also are true, resulting in the third output of Fig. 3.27.  Lines 24, 28, 33 and 38 also append the value vbCrLf to the TextBox.  This predefined value, known as a constant, positions the output cursor (the location where the next output character will be displayed) at the beginning of the next line in the TextBox. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

41  Indentation in If … Then Statements ◦ Notice the indentation of the body statements within the If … Then statements throughout the program. ◦ Such indentation enhances program readability. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

42

43  Handling the TextChanged Events for number1TextBox and number2TextBox ◦ After comparing two numbers and clicking the Compare Button, the resultTextBox shows the results of comparing the two values. ◦ If the user wishes to compare different values and starts typing in number1TextBox or number2TextBox, the previous results will still be displayed in the resultTextBox. ◦ This can be confusing to the program’s user. ◦ To prevent this problem, you can handle number1TextBox ’s and number2TextBox ’s TextChanged events and use them to clear the contents of the resultTextBox. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

44  The TextChanged event is a TextBox ’s default event.  Lines 43–47 and 50–54 show the TextChanged event handlers for number1TextBox and number2TextBox.  These methods are called when the user types in the corresponding TextBox es.  In both cases, we call the resultTextBox ’s Clear method, which removes the text that is currently displayed in the TextBox.  You can also clear a Label ’s or TextBox ’s Text property by assigning it the value String.Empty, which represents a string that does not contain any characters. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

45  Building the GUI ◦ Use the techniques you learned in Section 3.4 to build the GUI for the Comparison program. ◦ Figure 3.28 shows the GUI with all of its variable names. ◦ To allow the resultTextBox to display multiple lines, set its MultiLine property to True. ◦ In the Windows Forms designer, double click the compareButton to create its event handler. ◦ To create the Text-Changed event handlers for the number1TextBox and number2TextBox, double click each one in the Windows Forms designer— TextChanged is the default event for a TextBox. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

46

47  Entering the Code; Introducing the Parameter Info Window ◦ Enter the code from Fig. 3.27 into the three event handlers to complete the application. ◦ When you’re typing line 16, the IDE displays the Parameter Info window (Fig. 3.29) as you type the opening left parenthesis character, (, after resultTextBox.AppendText. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

48  This window contains information about the method and the information that the method needs to perform its task— its so-called parameter (methods can have several parameters).  In this case, the parameter info window shows that the method requires you to give it the text to append to the current contents of the TextBox.  The information you provide when you call the method is the method’s argument. ◦ Testing the Program  Be sure to test your program.  Enter the values shown in the sample outputs of Fig. 3.27 to ensure that the program is working properly. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

49

50  Operator Precedence ◦ Figure 3.30 shows the precedence of the operators introduced in this chapter. ◦ The operators are displayed from top to bottom in decreasing order of precedence. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

51


Download ppt "Visual Basic 2010 How to Program © 1992-2011 by Pearson Education, Inc. All Rights Reserved."

Similar presentations


Ads by Google