Building Visual Basic Application
Building Visual Basic Application
₦7,000
The top of the source code window consists of a list of objects and their associated events or
procedures. In the source code window, the object displayed is Form1 and the associated procedure is
Load.
≡
When you click on the object box, the drop-down list will display a list of objects you have inserted into
your form, as shown in figure 2.2. Here, you can see a form with the name Form1, a command button
≡
with the name Command1, a Label with the name Label1 and a Picture Box with the name Picture1.
Similarly, when you click on the procedure box, a list of procedures associated with the object will be
displayed , as shown in Figure 2.3. Some of the procedures associated with the object Form1 are
Activate, Click, DblClick (which means Double-Click) , DragDrop, keyPress and more. Each object has
its own set of procedures. You can always select an object and Writing the Codes for any of its
procedure in order to perform certain tasks.
≡ Figure 2.3 List of Procedures
You do not have to worry about the beginning and the end statements (i.e. Private Sub
Form_Load.......End Sub.); Just key in the lines in between the above two statements exactly as are
shown here. When you press F5 to run the program, you will be surprised that nothing showed up .In
order to display the output of the program, you have to add the Form1.show statement like in Example
2.1.1 or you can just use Form_Activate ( ) event procedure as shown in example 2.1.2. The
command Print does not mean printing using a printer but it means displaying the output on the
computer screen. Now, press F5 or click on the run button to run the program and you will get the
output as shown in Figure 2.4.
You can also perform arithmetic calculations as shown in Example 2.1.2. VB uses * to denote the
multiplication operator and / to denote the division operator. The output is shown in Figure 2.5, where
the results are arranged vertically.
Example 2.1.1
Example 2.1.2
You can also use the + or the & operator to join two or more texts (string) together like in example
2.1.4 (a) and (b)
Example 2.1.4(a)
Example 2.1.4(b)
Private SubForm_Load ( )
A = "Tom"
B = "likes"
C = "to"
D = "eat"
E = "burger"
Print A & B & C & D & E
End Sub
Buy Now
≡
Figure 2.6
≡
In this example, we want to show you how to Writing the Code to change the background and the
foreground color randomly. We will place two command buttons and a label on the form. One of the
command buttons will be used to change the background color while the other one will be used to
change the foreground color. The Label is for displaying the foreground color. There are two events
here, change background color and change foreground color. Therefore, we need to Writing the Code
for the two event procedures.
To make the program more interesting, we will use the Rnd() function, the Int() function and the RGB
codes to change the color randomly. The Rnd() function creates a random number between 0 and 1
and the RGB code uses a combination of three integers to form a certain color. The Int() is a function
that converts a number into an integer by truncating its decimal part and the resulting integer is the
largest integer that is smaller than the number. For example, Int(0.2)=0, Int(2.4)=2, Int(4.8)=4.
Therefore, Int(Rnd()*256) returns the smallest integer 0 and the biggest integer 255. The format of
RGB code is RGB(a,b,c), where a, b, c range from 0 to 255. For example, RGB(255,0,0) is red,
RGB(255,255,255) is white and (0,0,0) is black. Do not worry about the jargons, you will learn them in
later lesson.
Next, change the caption of the Label to "Please Change My Color". In addition, change the caption of
Command1 button to "Change Background Color" and change the caption of Command2 button to
"Change Foreground Color"
When you run the program, each time you press on the 'Change Background Color' button, you will see
different background color. Similarly, each time you press on the 'Change Foreground Color', you will
see the message on the Label changes color. The output is shown in Figure 2.7.
≡
Figure 2.7
≡
✕
119
Buy Now