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

Slides:



Advertisements
Similar presentations
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved. - edited by Maysoon Al-Duwais1.
Advertisements

Microsoft Visual Basic: Reloaded Chapter Seven More on the Repetition Structure.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
 2002 Prentice Hall. All rights reserved. 1 Outline 3.1Introduction 3.2Simple Program: Printing a Line of Text 3.3Another Simple Program: Adding Integers.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming
Introduction to scripting
Slide 1 Chapter 2 Visual Basic Interface. Slide 2 Chapter 2 Windows GUI  A GUI is a graphical user interface.  The interface is what appears on the.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Visual Basic Chapter 1 Mr. Wangler.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 6 Enhancing the Inventory Application Introducing Variables, Memory Concepts and.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
© 2006 Lawrenceville Press Slide 1 Chapter 3 Visual Basic Interface.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
 Application – another name for a program.  Interface – is what appears on the screen when the application is running.  Program Code – is instructions.
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 1B) UTPA – Fall 2011.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Security Panel Application Introducing the Select Case Multiple-Selection Statement.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved. - edited by Maysoon Al-Duwais1.
Introduction to Visual Basic Programming. Introduction Simple Program: Printing a Line of Text Another Simple Program: Adding Integers Memory Concepts.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Inventory Application.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
REEM ALMOTIRI Information Technology Department Majmaah University.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Visual Basic.NET Windows Programming
A variable is a name for a value stored in memory.
Chapter 6 JavaScript: Introduction to Scripting
Chapter 2 - Introduction to C Programming
Chapter 2: Introduction to Visual Basic Programming
Introduction to Scripting
Chapter 2 - Introduction to C Programming
Variables and Arithmetic Operations
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
WEB PROGRAMMING JavaScript.
Chapter 2 - Introduction to C Programming
Chapter 3: Introduction to Problem Solving and Control Statements
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Presentation transcript:

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

 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. © by Pearson Education, Inc. All Rights Reserved.

 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). © by Pearson Education, Inc. All Rights Reserved.

 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. © by Pearson Education, Inc. All Rights Reserved.

 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. © by Pearson Education, Inc. All Rights Reserved.

 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. © by Pearson Education, Inc. All Rights Reserved.

 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. © by Pearson Education, Inc. All Rights Reserved.

 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. © by Pearson Education, Inc. All Rights Reserved.

 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. © by Pearson Education, Inc. All Rights Reserved.

 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. © by Pearson Education, Inc. All Rights Reserved.

 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!".” © by Pearson Education, Inc. All Rights Reserved.

 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. © by Pearson Education, Inc. All Rights Reserved.

 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. © by Pearson Education, Inc. All Rights Reserved.

 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. © by Pearson Education, Inc. All Rights Reserved.

 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. © by Pearson Education, Inc. All Rights Reserved.

 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). © by Pearson Education, Inc. All Rights Reserved.

 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 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. © by Pearson Education, Inc. All Rights Reserved.

◦ 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. © by Pearson Education, Inc. All Rights Reserved.

◦ 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. © by Pearson Education, Inc. All Rights Reserved.

 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. © by Pearson Education, Inc. All Rights Reserved.

 When floating-point numbers (that is, numbers that contain a decimal point, such as and – ) 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. © by Pearson Education, Inc. All Rights Reserved.

 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. © by Pearson Education, Inc. All Rights Reserved.

 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. © by Pearson Education, Inc. All Rights Reserved.

 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. © by Pearson Education, Inc. All Rights Reserved.

 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. © by Pearson Education, Inc. All Rights Reserved.

 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. © by Pearson Education, Inc. All Rights Reserved.

 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. © by Pearson Education, Inc. All Rights Reserved.

 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  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. © by Pearson Education, Inc. All Rights Reserved.

 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. © by Pearson Education, Inc. All Rights Reserved.

 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. © by Pearson Education, Inc. All Rights Reserved.

 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. © by Pearson Education, Inc. All Rights Reserved.

 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. © by Pearson Education, Inc. All Rights Reserved.

 Entering the Code; Introducing the Parameter Info Window ◦ Enter the code from Fig 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. © by Pearson Education, Inc. All Rights Reserved.

 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 to ensure that the program is working properly. © by Pearson Education, Inc. All Rights Reserved.

 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. © by Pearson Education, Inc. All Rights Reserved.