► Create, modify, and program a TextBox object► Use code to place data in the Text property of a Label object ► Use the AcceptButton and CancelButton properties ► Understand and declare
Trang 1Microsoft Visual Basic 2015
CHAPTER FOUR
Variables and
Arithmetic Operations
Trang 2► Create, modify, and program a TextBox object
► Use code to place data in the Text property of a Label object
► Use the AcceptButton and CancelButton properties
► Understand and declare String and Numeric variables
►Use assignment statements to place data in variables
Trang 3► Use literals and constants in coding statements
► Understand scope rules for variables
► Convert string and numeric data
►Understand and use arithmetic operators and arithmetic operations
Trang 4► Format and display numeric data as a string
► Create a form load event
► Create a concatenated string
►Debug a program
Trang 8© 2016 Cengage Learning® May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part
Sizing and Positioning a TextBox Object
► Select the TextBox object Select the (Name) property and type the desired name Scroll in the Properties window until the Text property is visible and then tap or click the right column for the Text property
► Type the maximum number of characters the user normally will enter into the text box and then press the ENTER key Programmers often use the digit 8 in this situation because it is wider than other digits
Trang 9© 2016 Cengage Learning® May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part
Sizing and Positioning a TextBox Object
► Using the Properties window, change the Font property to the correct font and font size Then, drag the right edge of the TextBox object and resize it to be slightly wider than the entry
► To horizontally align the text in the label and the text in the text box, drag the text box
up until a red snap line indicates the bottoms of the text are aligned Then, release the left mouse button
Trang 10© 2016 Cengage Learning® May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part
Sizing and Positioning a TextBox Object
Trang 11© 2016 Cengage Learning® May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part
Aligning Text in a TextBox Object
► Select the TextBox object In the Properties window, scroll until the TextAlign property is visible, tap
or click the TextAlign property in the left column, and then tap or click the list arrow in the right column of the TextAlign property
► Tap or click the desired alignment option in the TextAlign property list
► Because the TextBox object is sized properly, remove the digits in the TextBox object Select the characters in the Text property, press the DELETE key on your keyboard, and then press the
ENTER key
Trang 12© 2016 Cengage Learning® May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part
Aligning Text in a TextBox Object
Trang 13© 2016 Cengage Learning® May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part
Creating a MultiLine Text Box
► Select the TextBox object, tap or click the Action tag, and then point to the MultiLine check box
►Tap or click the MultiLine check box
Trang 14© 2016 Cengage Learning® May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part
Creating a MaskedTextBox Object
Trang 15© 2016 Cengage Learning® May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part
Creating a MaskedTextBox Object
► Drag a MaskedTextBox NET component from the Toolbox to the Windows Form
object Then, tap or click the Action tag on the TextBox object and point to the Set
Mask command
► Tap or click Set Mask on the MaskedTextBox Tasks list and then tap or click the
desired mask description in the Input Mask dialog box
► Tap or click the OK button in the Input Mask dialog box and then tap or click anywhere
in the Windows Form object
Trang 16© 2016 Cengage Learning® May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part
Creating a MaskedTextBox Object
Trang 17►Drag the Label object up until the red snap line appears Then release the Label object
Trang 19© 2016 Cengage Learning® May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part
Accept Button in Form Properties
► After the Button objects are added, tap or click a blank area in the Windows Form object to select it Scroll in the Properties window until the AcceptButton property is visible Tap or click the AcceptButton property name in the left column and then tap or click the AcceptButton property list arrow in the right column
►Tap or click the desired button in the AcceptButton property list
Trang 20© 2016 Cengage Learning® May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part
Accept Button in Form Properties
Trang 21© 2016 Cengage Learning® May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part
Cancel Button in Form Properties
► When the user presses the esc key, the event handler processing will be executed for the button identified as the Cancel button
► Tap or click a blank area in the Windows Form object to select it
► Tap or click the CancelButton property name in the left column in the Properties
window for the Windows Form object, and then tap or click the CancelButton list arrow
►Tap or click the desired button name in the CancelButton property list
Trang 22© 2016 Cengage Learning® May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part
Visual Studio Preparation for Code Entry
►With the Toolbox visible, tap or click the Toolbox Close button The Toolbox closes and the work area expands in size To display the Toolbox after it has been closed, tap or click the View menu and then tap or click Toolbox
Trang 23© 2016 Cengage Learning® May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part
String Data Type
► A String data type can store any character available on the computer
► The data type for the value the user enters in a TextBox object and that is stored in the Text property
of the TextBox object is string
► A variable is a named location in RAM where data is stored
► A String variable is a named location in RAM that can store a string value
Trang 24© 2016 Cengage Learning® May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part
String Data Type
Trang 26► Press the SPACEBAR, type the word As, and then press the SPACEBAR again
► Because the entry should be String, type str
►Press the ENTER key
Trang 27► Press the SPACEBAR, press the EQUAL SIGN key, and then press the SPACEBAR
► Type the name of the text box containing the value you wish to use
► Press the PERIOD key and then, if necessary, type te to highlight the Text entry in the IntelliSense list
► Press the ENTER key
Trang 29© 2016 Cengage Learning® May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part
Numeric Data Types
►A numeric data type must be used in arithmetic operations
Trang 30© 2016 Cengage Learning® May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part
Numeric Data Types
Trang 31© 2016 Cengage Learning® May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part
Other Data Types
Trang 32© 2016 Cengage Learning® May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part
Other Data Types
Trang 33© 2016 Cengage Learning® May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part
Miscellaneous Data Types
Trang 36© 2016 Cengage Learning® May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part
Forced Literal Types
►Sometimes you might want a literal to be a different data type than the Visual Basic default type
Trang 38► The declaration of a constant begins with the letters Const, not the letters Dim
► You must assign the value to be contained in the constant on the same line as its definition
► You cannot attempt to change the value of the constant anywhere in the program If you do, you will produce a compiler error
► The letter c often is placed before the constant name to identify it throughout the program as a variable that cannot be changed
► Other than the letter c, constant names are formed using the same rules and techniques as other variable names
Trang 40► The scope of a variable specifies where the variable can be referenced in a Visual Basic statement
within the program
► You declare a variable in the region within a program
► The code between the Sub statement and the End Sub statement is a procedure
► A variable that can only be referenced within the region of the program where it is defined is called a
local variable
• Local variables have a certain lifetime in the program
► Global variables can be used in multiple regions of a Visual Basic program
Trang 41© 2016 Cengage Learning® May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part
Converting Variable Data
► Visual Basic includes several procedures that allow you to convert one data type to another data type
► A procedure to convert a String data type to an Integer data type is named ToInt32
►The procedure is in the Convert class, which is available in a Visual Studio 2015 class library
Trang 42► A procedure that returns a value is called a Function procedure, or a function
► An argument identifies a value required by a procedure
►Every procedure is part of a class
Trang 44► To prevent automatic conversion of values, the developer must insert the Option Strict On statement in the program prior to any event handler code
► The Option Strict On statement explicitly prevents any default data type conversions that would cause data loss and prevents any conversion between numeric types and strings
Trang 48• Exponentiation (^) is performed first
• Multiplication (*) and division (/) are performed next
• Integer division (\) is next
• MOD then occurs
• Addition (+) and subtraction (-) are performed last
• Within these five steps, calculations are performed left to right
Trang 49© 2016 Cengage Learning® May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part
Displaying Numeric Output Data
Trang 50► Use the format specifier to identify the format for the numeric data to be returned by
the ToString function
Trang 51► The precision specifier is a number included within the quotation marks in the
function call that identifies the number of positions that should be returned to the right
of the decimal point
Trang 53© 2016 Cengage Learning® May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part
Clearing the Text Property of a Label
► The Clear procedure cannot be used with a Label object
►You must write an assignment statement that assigns a null length string to the Text property
Trang 54© 2016 Cengage Learning® May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part
Setting the Focus
►When the focus is on a TextBox object, the insertion point is located in the text box
Trang 55© 2016 Cengage Learning® May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part
Form Load Event
►A form load event occurs when the program starts and Windows Form Object is loaded into computer memory
Trang 56► The process of joining two different values into a single string is called concatenation
►The values being concatenated must be String data types
Trang 59© 2016 Cengage Learning® May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part
Debugging Your Program
► A Format Exception occurs when the
user enters data that a statement
within the program cannot process
properly
Trang 60© 2016 Cengage Learning® May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part
Debugging Your Program
► An Overflow Exception occurs when the user enters a value greater than the
maximum value the statement can process
► It is not possible to divide by zero, so if your program contains a division operation
and the divisor is zero, the Divide By Zero Exception will occur
Trang 63© 2016 Cengage Learning® May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part
Event Planning Document
Trang 64► Create, modify, and program a TextBox object
► Use code to place data in the Text property of a Label object
► Use the AcceptButton and CancelButton properties
► Understand and declare String and Numeric variables
►Use assignment statements to place data in variables
Trang 65► Use literals and constants in coding statements
► Understand scope rules for variables
► Convert string and numeric data
►Understand and use arithmetic operators and arithmetic operations
Trang 66► Format and display numeric data as a string
► Create a form load event
► Create a concatenated string
►Debug a program
Trang 67Microsoft Visual Basic 2015
CHAPTER FOUR COMPLETE
Variables and
Arithmetic Operations