VB2022 VB2019 VB6 VB Sample Code About Us

Lesson 2 : Creating Visual Basic 6 Application


2.1 Creating Your First Application

Launching the VB6 IDE

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.

Accessing the Code Window

Double-click on Form1 to open its source code window, as shown in Figure 2.1.

Visual Basic 6 source code window
Figure 2.1 The VB6 Source Code window

Understanding the Object and Procedure Boxes

At the top of the code window, you'll find two drop-down lists, as shown in Figure 2.2 and Figure 2.3:

List of visual basic 6 objects
Figure 2.2: List of Objects
Figure 2.3 List of Procedures

Writing Your First Code

You don't need to worry about the Private Sub and End Sub statements; just enter your code between them, For example:

Example 2.1.1

Private Sub Form_Load ( )
Form1.show
 Print "Welcome to Visual Basic tutorial"
End Sub
Running this code (press F5) will display the message in the form, as shown in Figure 2.4

Performing Arithmetic Operations

You can perform arithmetic calculations using the Print statement:

Example 2.1.2

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.

Figure 2.4 : The output of Example 2.1.1
Figure 2.5 : The output of Example 2.1.2

Concatenating Strings

Use the + or & operator to join strings:

Example 2.1.4(a)

Private Sub Form_Load ( )
 A = "Tom"
 B = "likes"
 C = "to"
 D = "eat"
 E = "burger"
 Print A + B + C + D + E
End Sub

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


The Output of Example 2.1.4(a) &(b) is as shown in Figure 2.6.

Figure 2.6

2.2 Steps in Building a Visual Basic Application

Step 1: Design the interface

Step 2:Writing the Code for event procedures

Example 2.2 Changing Background and Foreground Color Randomly

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 Sub
Running this program allows users to change the form's background and the label's foreground colors randomly, as shown in Figure 2.7.

Figure 2.7

Summary

✅ In This Lesson, You Learned:

Next Lesson

Ready to learn about using controls? Continue to Lesson 3: Working with Controls.

Related Resources





Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy

Last update:05/15/2025 14:25:08