chapter 3
DESCRIPTION
Chapter 3. Variables and Calculations. The TextBox Control. A text box is a rectangular area on a form that accepts input from a keyboard. Public Class Form1 Private Sub Button1_Click( ByVal sender As System.Object , ByVal e As System.EventArgs ) Handles Button1.Click - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Chapter 3](https://reader034.vdocument.in/reader034/viewer/2022042901/56813c51550346895da5d1da/html5/thumbnails/1.jpg)
Addison Wesley is an imprint of
© 2011 Pearson Addison-Wesley. All rights reserved.
Addison Wesley is an imprint of
Chapter 3
Variables and Calculations
![Page 2: Chapter 3](https://reader034.vdocument.in/reader034/viewer/2022042901/56813c51550346895da5d1da/html5/thumbnails/2.jpg)
Copyright © 2011 Pearson Addison-Wesley
The TextBox Control
• A text box is a rectangular area on a form that accepts input from a keyboard
Chapter 3- Slide 2
![Page 3: Chapter 3](https://reader034.vdocument.in/reader034/viewer/2022042901/56813c51550346895da5d1da/html5/thumbnails/3.jpg)
Copyright © 2011 Pearson Addison-Wesley
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click Label2.Text = "hello " & TextBox1.Text End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close() End SubEnd Class
Chapter 3- Slide 3
![Page 4: Chapter 3](https://reader034.vdocument.in/reader034/viewer/2022042901/56813c51550346895da5d1da/html5/thumbnails/4.jpg)
Copyright © 2011 Pearson Addison-Wesley
Using the Text Property in Code
• The TextBox control’s Text property can be accessed in code the same way you access other properties
• For Example:– The contents of the Text property can be assigned into
a Label control’s Text property:– lblInfo.Text = txtInput.Text– The contents of the Text property can be displayed in a
message box– MessageBox.Show(txtInput.Text)
Chapter 3- Slide 4
![Page 5: Chapter 3](https://reader034.vdocument.in/reader034/viewer/2022042901/56813c51550346895da5d1da/html5/thumbnails/5.jpg)
Copyright © 2011 Pearson Addison-Wesley
Clearing a Text Box
• Can be done with an assignment statement:– txtInput.Text = String.Empty– OR txtInput.Text = “”– assigning the predefined constant String.Empty replaces
whatever text was in txtInput with an empty string• Can also be done with a method:
– txtInput.Clear()– Clear is a Method, not a Property– Methods are actions – as in clearing the text– Uses the form Object.Method()
Chapter 3- Slide 5
![Page 6: Chapter 3](https://reader034.vdocument.in/reader034/viewer/2022042901/56813c51550346895da5d1da/html5/thumbnails/6.jpg)
Copyright © 2011 Pearson Addison-Wesley
String Concatenation
• Assume the user has entered their name into the TextBox txtName
• Label lblGreeting can say, “Hello” to any name found in the TextBox– lblGreeting.Text = "Hello " & txtName.Text– Appends user name in txtName.Text to “Hello ”
and stores result in text property of lblGreeting
Chapter 3- Slide 6
![Page 7: Chapter 3](https://reader034.vdocument.in/reader034/viewer/2022042901/56813c51550346895da5d1da/html5/thumbnails/7.jpg)
Copyright © 2011 Pearson Addison-Wesley
String Concatenation
• another example of how to concatenate strings from text boxes
Chapter 3- Slide 7
txtDayOfWeektxtMonthtxtDayOfMonthtxtYearlblDateString
btnExitbtnClearbtnShowDate
![Page 8: Chapter 3](https://reader034.vdocument.in/reader034/viewer/2022042901/56813c51550346895da5d1da/html5/thumbnails/8.jpg)
Copyright © 2011 Pearson Addison-Wesley Chapter 3- Slide 8
![Page 9: Chapter 3](https://reader034.vdocument.in/reader034/viewer/2022042901/56813c51550346895da5d1da/html5/thumbnails/9.jpg)
Copyright © 2011 Pearson Addison-Wesley
The Focus Method
• For a control to have the focus means that it is ready to receive the user's input
• In a running form, one and only one of the controls on the form may have the focus
• The focus can be set to a control in code using the Focus method:
txtUserName.Focus()
Chapter 3- Slide 9
![Page 10: Chapter 3](https://reader034.vdocument.in/reader034/viewer/2022042901/56813c51550346895da5d1da/html5/thumbnails/10.jpg)
Copyright © 2011 Pearson Addison-Wesley
The Focus Method
• You can tell which control has focus by its characteristics:– When a TextBox has focus, it will have a blinking
cursor or its text will be highlighted– When a button, radio button, or a check box has
focus, you’ll see a thin dotted line around the control
Chapter 3- Slide 10
![Page 11: Chapter 3](https://reader034.vdocument.in/reader034/viewer/2022042901/56813c51550346895da5d1da/html5/thumbnails/11.jpg)
Copyright © 2011 Pearson Addison-Wesley
Assigning Keyboard Access Keys to Buttons
• Say your form had a button with the text “Exit" on it
• You can allow the user to activate the button using Alt-X instead of a mouse click
• Just change the button text property to “E&xit"• The character following the '&' (x in this case) is
designated as an access key• Be careful not to use the same access key for two
different buttons
Chapter 3- Slide 11
![Page 12: Chapter 3](https://reader034.vdocument.in/reader034/viewer/2022042901/56813c51550346895da5d1da/html5/thumbnails/12.jpg)
Copyright © 2011 Pearson Addison-Wesley
'&' Has Special Meaning in a Button
• Note that the '&' in “E&xit" does not display in the button control on the form
• It simply establishes the Alt Key access
• In order to actually display an '&' on a button, it must be entered as "&&“– Button text Save & Exit is
entered as Save && Exit
Chapter 3- Slide 12
![Page 13: Chapter 3](https://reader034.vdocument.in/reader034/viewer/2022042901/56813c51550346895da5d1da/html5/thumbnails/13.jpg)
Addison Wesley is an imprint of
© 2011 Pearson Addison-Wesley. All rights reserved.
Addison Wesley is an imprint of
Section 3.2
VARIABLES AND DATA TYPES
Variables hold data that may be manipulated, used to manipulateother data, or remembered for later use.
![Page 14: Chapter 3](https://reader034.vdocument.in/reader034/viewer/2022042901/56813c51550346895da5d1da/html5/thumbnails/14.jpg)
Copyright © 2011 Pearson Addison-Wesley
Declaring Variables
• A variable declaration is a statement that creates a variable in memory• The syntax is:
Dim VariableName As DataType– Dim (short for Dimension) is a keyword– VariableName is the programmer designated name– As is a keyword– DataType is one of many possible keywords for the type of value
the variable will contain
• Here is an example of a variable declaration:
Dim intLength as Integer
Chapter 3- Slide 14
![Page 15: Chapter 3](https://reader034.vdocument.in/reader034/viewer/2022042901/56813c51550346895da5d1da/html5/thumbnails/15.jpg)
Copyright © 2011 Pearson Addison-Wesley
Declaring Multiple Variables
• Several variables may be declared in one statement if they all hold the same type of value
Dim intLength, intWidth, intHeight as Integer
• Or this can be done in 3 separate statementsDim intLength as IntegerDim intWidth as IntegerDim intHeight as Integer
Chapter 3- Slide 15
![Page 16: Chapter 3](https://reader034.vdocument.in/reader034/viewer/2022042901/56813c51550346895da5d1da/html5/thumbnails/16.jpg)
Copyright © 2011 Pearson Addison-Wesley
Setting the Value of a Variable
• An assignment statement is used to set the value of a variable, as in:– Assign the value 112 to the variable length– intLength= 112– Assign the string literal “Good Morning “ followed
by the contents of the text box txtName to the variable greeting
– greeting = "Good Morning " & txtName.Text
Chapter 3- Slide 16
![Page 17: Chapter 3](https://reader034.vdocument.in/reader034/viewer/2022042901/56813c51550346895da5d1da/html5/thumbnails/17.jpg)
Copyright © 2011 Pearson Addison-Wesley
Visual Basic Data Types
• Integer types– Byte– Short– Integer– Long
• Floating-Point types– Single– Double– Decimal
• Other data types
– Boolean– Char– String– Date
Chapter 3- Slide 17
![Page 18: Chapter 3](https://reader034.vdocument.in/reader034/viewer/2022042901/56813c51550346895da5d1da/html5/thumbnails/18.jpg)
Copyright © 2011 Pearson Addison-Wesley
The String Data Type
• A string literal is enclosed in quotation marks– The following code assigns the name Jose Gonzales to
the variable strNameDim strName as stringstrName = "Jose Gonzales"
• An empty string literal can be coded as:– Two consecutive quotation marks
strName = ""– Or by the special identifier String.Empty
strName = String.Empty
Chapter 3- Slide 18
![Page 19: Chapter 3](https://reader034.vdocument.in/reader034/viewer/2022042901/56813c51550346895da5d1da/html5/thumbnails/19.jpg)
Copyright © 2011 Pearson Addison-Wesley
The Date Data Type
• Date data type variables can hold the date and time or both– You can assign a date literal to a Date variable, as shown
here:Dim dtmBirth As DatedtmBirth = #5/1/2010#
• A date literal is enclosed within # symbols– All of the following Date literals are valid:
#12/10/2010##8:45:00 PM##10/20/2010 6:30:00 AM#
Chapter 3- Slide 19
![Page 20: Chapter 3](https://reader034.vdocument.in/reader034/viewer/2022042901/56813c51550346895da5d1da/html5/thumbnails/20.jpg)
Copyright © 2011 Pearson Addison-Wesley
Default Values and Initialization
• When a variable is first created in memory, it is assigned a default value– numeric types are given a value of zero– Boolean types are given a value of False– strings are given a value of Nothing– dates default to 12:00:00 AM January 1,1
• Good practice to initialize string variables– Dim strName as String = String.Empty– String with value Nothing causes error if used
Chapter 3- Slide 20
![Page 21: Chapter 3](https://reader034.vdocument.in/reader034/viewer/2022042901/56813c51550346895da5d1da/html5/thumbnails/21.jpg)
Copyright © 2011 Pearson Addison-Wesley
Initialization of Variables
• Can provide a starting or initialization value for any type of variable in a Dim statement
• Usually want to set an initial value unless assigning a value prior to using the variable
• Just append = value to the Dim statement where value is the literal to be assigned to the variable
Dim intMonthsPerYear As Integer = 12
Chapter 3- Slide 21
![Page 22: Chapter 3](https://reader034.vdocument.in/reader034/viewer/2022042901/56813c51550346895da5d1da/html5/thumbnails/22.jpg)
Copyright © 2011 Pearson Addison-Wesley
Scope and Local Variables
• Scope refers to the part of the program where:– A variable is visible and– May be accessed by program code
• Variables declared within a procedure are called local variables and observe these characteristics– Scope begins where variable is declared– Extends to end of procedure where declared– Variable is not visible outside the procedure
• A variable cannot be declared twice in the same procedure
Chapter 3- Slide 22
![Page 23: Chapter 3](https://reader034.vdocument.in/reader034/viewer/2022042901/56813c51550346895da5d1da/html5/thumbnails/23.jpg)
Addison Wesley is an imprint of
© 2011 Pearson Addison-Wesley. All rights reserved.
Addison Wesley is an imprint of
Section 3.3
PERFORMING CALCULATIONS
Visual Basic has powerful arithmetic operators that perform calculationswith numeric variables and literals.
![Page 24: Chapter 3](https://reader034.vdocument.in/reader034/viewer/2022042901/56813c51550346895da5d1da/html5/thumbnails/24.jpg)
Copyright © 2011 Pearson Addison-Wesley
Common Arithmetic Operators
• Visual Basic provides operators for the common arithmetic operations:
+ Addition- Subtraction* Multiplication/ Division^ Exponentiation
Chapter 3- Slide 24
![Page 25: Chapter 3](https://reader034.vdocument.in/reader034/viewer/2022042901/56813c51550346895da5d1da/html5/thumbnails/25.jpg)
Copyright © 2011 Pearson Addison-Wesley
Common Arithmetic Operators
• AdditiondblTotal = dblPrice + dblTax
• SubtractiondblNetPrice = dblPrice – dblDiscount
• MultiplicationintArea = intLength * intWidth
• DivisiondblAverage = intTotal / intItems
• ExponentiationdblCube = dblSide ^ 3
Chapter 3- Slide 25