Basic Concepts
Visual Basic
Lección 03
String Concatenation
Assume the user has entered their name into the
TextBox txtName
Label lblGreetings can say, “Hello to any name
found in the TextBox
lblGreeting.Text = “Hello “ & txtName.Text
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
Only a control capable of receiving some sort of input
may have the focus
The focus can be set to a control in code using the
Focus method:
txtUserName.Focus()
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
Keyboard Access Keys in
Buttons
Say your form had a button with the text “Save” on it
You can allow the user to activate the button using Alt-S
instead of a mouse click
Just change the button text property to “&Save”
The character following the ‘&’ (S in this case) is
designated as an access key
Be careful not to use the same access key for two
different buttons
‘&’ Has Special Meaning
in a Button
Note that the ‘&’ in “&Save” 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
Declaring Variables
A variable declaration is a statement that creates a variable in
memory
This syntax is
Dim VariableName As DataType
Dim (short for Dimension) is a keyword
VariableName is the programmer designeted name
As is a keyword
DataType is one of many possible keywords for the type of
value the variable will contain
Example: Dim Length as Integer
Declaring Multiple Variables
Several variables maybe declared in one statement
if they all hold the same type of value
Dim Length, Width, Height as Integer
Or this can be done in 3 separate statements
Dim Length as Integer
Dim Width as Integer
Dim Height as Integer
Visual Basic Data Types
Integer types • Other data types
Byte • Boolean
Short • Char
Integer • String
Long • Date
Floating-Point types
Single
Double
Decimal
Formatting Date and time
using predefined formats
Date and time can be formatted using predefined
formats and also user-defined formats. The
predefined formats of date and time are shown in
Table.
Example 01
Formatting Date and time
using user-defined formats
Beside using the predefined formats, you can also
use the user-defined formatting functions. The
general format of a user-defined for date/time is:
Format (expression,style)
Example 02
Explicit Type Conversion
The following narrowing conversions require an
explicit type conversion
Double to Single
Single to Integer
Long to Integer
Boolean, Date, Object, String and numeric types
represent different sorts of values and require
conversion functions as well
Explicit Type
Conversion Examples
Rounding can be done with the CInt function
Count = CInt(12.4) ‘Count value is 12
Count = Cint(12.5) ‘ Count value is 13
CStr converts an integer value to a string
Dim text as String = CStr(26)
CDec converts a string to a decimal value
Dim Pay as Decimal = Cdec(“$1,500”)
CDate converts a string to a date
Dim Hired as Date = Cdate(“05/10/2014”)
A full List of Conversion
Functions
There are conversion functions for each data type:
CBool(expression) CLng(expression)
CByte(expression) CObj(expression)
CChar(expression) CSByte(expression)
CDate(expression) CShort(expression)
CDbl(expression) CSng(expression)
CDec(expression) CStr(expression)
CInt(expression)
Common Arithmetic Operators
Visual Basic provides operators for the common
arithmetic operations:
+ Addition
- Subtraction
* Multiplication
/ Division
^ Exponentiation
Common Arithmetic Operators
Addition
Total = Price + Tax
Subtraction
NetPrice = Price – Discount
Multiplication
Area = Length * Width
Division
Average = Total / Items
Exponentiation
Cube = Side ^ 3
Instrucciones
Desarrolle los siguientes programas en Visual
Basic, escriba el código necesario para poder
ejecutar cada programa.
Recuerde colocar el nombre indicado a cada objeto
creado.
Laboratorio 05
Laboratorio 06
Laboratorio 07
Desarrolle un programa en VBasic que calcule su
edad e imprima la edad en pantalla.