Professional Documents
Culture Documents
Lecture6 VARIABLES
Lecture6 VARIABLES
Lecture6 VARIABLES
EVENT PROCEDURE
When you create a program in Visual Basic, you'll generally be doing event-driven
programming. Event-driven programming means that most of the code you write will
be run as users do things within your program or even when certain things happen in
Windows--when events occur. Of course, programming this way means that you have
to know when events occur and have to write code that will make your program do
something in response to the event.
Event Occurrence
GotFocus An object receives focus. (A text box has the focus whenever it is ready
to accept typing; that is, whenever it contains a blinking cursor.)
KeyDown The user presses a keyboard key (any key) while an object has focus.
KeyPress The user presses and releases a keyboard key(any key) while an object
has focus.
KeyUp The user releases a keyboard key while an object has focus.
Event Occurrence
MouseDown The user presses any mouse button while the mouse pointer is over an
object.
MouseUp The user releases any mouse button while the mouse pointer is over an
object.
Example:
1. Create an interface as the below form:
TextBox
Command
2. Double-click on the text box. A window, called the Code window, appears. Just
below the title bar are two drop-down list boxes. The left box is called the Object
box and the right box is called the Procedure box. (When you position the mouse
pointer over one of these list boxes, its type appears.)
Computer Principles II - Asst. Lect. Zahraa A. Nejim
6. Once running the program the textBox text becomes green and the font size equals
to 24. This calls the event procedure Text1_GotFocus since the cursor is inside the
TextBox1.
Private Sub Text1_GotFocus()
Text1.Font.Size = 24
Text1.ForeColor = vbGreen
Text1.FontItalic = False
End Sub
7. Press the Tab key (in your keyboard). The contents of the text box will be italic and
the ForeColor is blue. When Tab was pressed, the TextBox lost the focus; that is, the
.
event LostFocus happened to TextBox1 Thus, the event procedure Text1_ LostFocus
was called, and the code inside the procedure was
executed.
Private Sub Text1_LostFocus()
Text1.Font.Italic = True
Text1.ForeColor = vbBlue
End Sub
8. Press the Tab key again to move the cursor (and, therefore, the focus) to the text box.
This calls the event procedure Text1_GotFocus.
9. Click on the TextBox. This calls the event procedure Text1_ Click(), which change text
color to red and bold formatting .
Private Sub Text1_Click()
Text1.ForeColor = vbRed
Text1.Font.Bold = True
End Sub
10. Keep the cursor in the TextBox1 and press any key in your keyboard for example
Shift. The Text1_KeyUp event procedure will be activated and will show the below
message:
Private Sub Text1_KeyUp(KeyCode As Integer,
Shift As Integer)
MsgBox ("Text key Up Event (ANY keyboard
key)")
End Sub
11. Adding or deleting characters in the TextBox1 will activate the Text1 Change event
procedure, which will change the TextBox font size to 20.
Private Sub Text1_Change()
Text1.Font.Size = 20
End Sub
Computer Principles II - Asst. Lect. Zahraa A. Nejim
NUMBER
Numeric data are data that consists of numbers, which can be computed
mathematically with various standard operators such as add, minus, multiply, divide
and so on. In Visual Basic, the numeric data are divided into 7
types, they are summarized in the below Table: Type Storage
Byte 1 byte
Integer 2 bytes
1. Numbers must not contain commas, dollar signs, or percent Long 4 bytes
signs. Also, mixed numbers, such as 8 1/2, are not allowed.
Single 4 bytes
Arithmetic Operations
The five arithmetic operations in Visual Basic are addition, subtraction, multiplication,
division, and exponentiation. Addition, subtraction, and division are denoted in Visual
Basic by the standard symbols +, –, and /, respectively. However, the notations for
multiplication and exponentiation differ from the customary mathematical notations.
a·b or a x b a*b
a^r
ar
Computer Principles II - Asst. Lect. Zahraa A. Nejim
(The asterisk [*] is the upper character of the 8th key. The caret [^] is the upper
character of the 6th key.)
Picture1.Print n
displays the number n in the picture box. Another important method is Cls. The
statement
Picture1.Cls
Example:
• Draw the following VB6 interface:
• Name the PictureBox1 as picResults.
• Write the below source code:
PictureBox
picResults.Cls
picResults.Print 3 + 2 Command
picResults.Print 3 - 2
picResults.Print 3 * 2
picResults.Print 3 / 2
picResults.Print 3 ^ 2
picResults.Print 2 * (3 + 4)
• Run the program and the output is as following:
SCIENTIFIC NOTATION
Let us review powers of 10 and scientific notation.
Our method of decimal notation is based on a
systematic use of exponents:
Computer Principles II - Asst. Lect. Zahraa A. Nejim
In Visual Basic, b·10r is usually written as bEr. (The letter E is an abbreviation for
exponent.) The following forms of the numbers just mentioned are equivalent.
The process of calculating ar is called raising a to the rth power. Some other types of
exponents are the following:
Example:
The following program illustrates scientific notation. The computer’s choice of whether
to display a number in scientific or standard form depends on the magnitude of the
number.
picResults.Cls
picResults.Print 1.2 * 10 ^ 34
picResults.Print 1.2 * 10 ^ 8
picResults.Print 1.2 * 10 ^ 3
End Sub
Conditional Operators
To control the VB program flow, we can use various conditional operators. Basically,
they resemble mathematical operators. Conditional operators are very powerful tools,
they let the VB program compare data values and then decide what action to take,
whether to execute a program or terminate the program and etc. These operators are
shown in below Table:
Logical Operators
In addition to conditional operators, there are a few logical operators which offer added
power to the VB programs. There are shown in below Table:
Operator Meaning
And Both sides must be true
or One side or other must be true
Xor One side or other must be true but not both
Not Negates truth
Computer Principles II - Asst. Lect. Zahraa A. Nejim
VARIABLES
Variables are used by Visual Basic to hold information needed by your application. Rules
used in naming variables:
Variable Declaration
• Most major programming languages require that all variables be declared
before they can be used. Although declaring variables with Dim statements is
optional in Visual Basic, you can tell Visual Basic to make declaration
mandatory. The steps are as follows:
a. From any code window, click on the down-arrow to the right of the Object
box and click on (General).
Computer Principles II - Asst. Lect. Zahraa A. Nejim
b. Type
Option Explicit
• Then, if you use a variable without first declaring it in a Dim statement, the
message “Variable not defined” will appear as soon as you attempt to run the
program. One big advantage of using Option Explicit is that mistypings of
variable names will be detected. Otherwise, malfunctions due to typing errors
are often difficult to detect.
• Variables are normally declared in the general section of the codes' windows
using the Dim statement. The format is as follows:
For Example:
Dim password As String
• Variables that are not (explicitly) declared with Dim statements are said to be
implicitly declared. Such variables, which have a data type called Variant, can
hold strings, numbers, and several other kinds of information.
• For string declaration, there are two possible formats, one for the variable-
length string and another for the fixed-length string. For the variable-length
Computer Principles II - Asst. Lect. Zahraa A. Nejim
string, just use the same format above. However, for the fixed-length string,
you have to use the format as shown below:
Dim yourName as String * 10, yourName can holds no more than 10 Characters.
1. Default
2. Implicit
3. Explicit
• Global level variables retain their value and are available to all procedures
within an application. Global declarations are only allowed in the General
Modules. Module level variables are declared in the declarations part of the
general object of a module's code window. (It is advisable to keep all global
variables in one module.) Use the Global keyword:
Global MyInt as Integer
Global MyDate as Date
Computer Principles II - Asst. Lect. Zahraa A. Nejim
• Constants, once declared and initialized cannot be changed (hence the name
constant' and are declared using the Visual Basic Const keyword. The syntax
for declaring variables is as follows:
• When a variable is declared with Procedure level scope it only exists while the
code in the corresponding procedure is executing. Once the procedure
completes, the variable and the variable assigned to it are destroyed. Under
certain circumstance it may be necessary for the variable and the current value
assign to it to persist beyond the life of the procedure. Next time the
procedure is called, therefore, the variable still holds the value it held on the
previous invocation of the procedure. The syntax to declare a static variable is
as follows: