First, launch the Microsoft Visual Basic 6 compiler. In the New Project dialog, select Standard EXE to enter the Visual Basic 6 Integrated Development Environment (IDE). A default form named Form1 will appear.
Double-click on Form1 to open its source code window, as shown in Figure 2.1.
At the top of the code window, you'll find two drop-down lists, as shown in Figure 2.2 and Figure 2.3:
You don't need to worry about the Private Sub and End Sub statements; just enter your code between them, For example:
Private Sub Form_Load ( ) Form1.show Print "Welcome to Visual Basic tutorial" End SubRunning this code (press F5) will display the message in the form, as shown in Figure 2.4
You can perform arithmetic calculations using the Print statement:
Private Sub Form_Activate ( ) Print 20 + 10 Print 20 - 10 Print 20 * 10 Print 20 / 10 End Sub
press F5 or click on the run button to run the program and where the results are arranged vertically, as shown in Figure 2.5.
Use the + or & operator to join strings:
Private Sub Form_Load ( ) A = "Tom" B = "likes" C = "to" D = "eat" E = "burger" Print A + B + C + D + E End Sub
Private SubForm_Load ( ) A = "Tom" B = "likes" C = "to" D = "eat" E = "burger" Print A & B & C & D & E End Sub
The
Output of Example 2.1.4(a) &(b) is as shown in Figure 2.6.
In this example, we'll create a form with two command buttons and a label:
We'll use the Rnd(), Int(), and RGB() functions to generate random colors.
Private Sub Command1_Click() Randomize R = Int(Rnd * 256) G = Int(Rnd * 256) B = Int(Rnd * 256) Form1.BackColor = RGB(R, G, B) End
Private Sub Command2_Click() Randomize R = Int(Rnd * 256) G = Int(Rnd * 256) B = Int(Rnd * 256) Label1.ForeColor = RGB(R, G, B) Label1.Caption = "Foreground Color Changed" End SubRunning this program allows users to change the form's background and the label's foreground colors randomly, as shown in Figure 2.7.
Print
statement+
and &
Rnd()
, Int()
, and RGB()
to change colors randomlyForm_Load
and Command_Click
Ready to learn about using controls? Continue to Lesson 3: Working with Controls.
Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy
Last update:05/15/2025 14:25:08