Lesson 2: Building Visual Basic 6 Applications


Custom Search

 

<Previous Lesson>  <<Home>> <Next Lesson>

2.1 Understanding Properties, Events and Methods

Before we start building a Visual Basic 6 application, we need to understand the basic concepts of properties , events and methods. Every object in Visual Basic 6, such as a form , a command button , a text box and more have a set of properties that describe them. Setting the objects' properties is the first step in building a Visual Basic application, i.e. designing the interface. We will learn more about setting properties in the lesson 3

The next step in building a Visual Basic application is to write code to response to the events. Events usually comprises actions triggered by the user, such as clicking the mouse buttons, pressing a key on the keyboard, dragging an object and more. Some of the common events are listed in Table 2.1 below:

 
Event Description
Click The user clicks the mouse button on an object.
DblClick The user double-clicks the mouse button on an object.
DragDrop The user drags an object to another location.
GotFocus An object receives focus.
KeyDown The user presses a keyboard key.
KeyPress The user presses and releases a keyboard key .
KeyUp The user releases a keyboard key while an object has focus.
LostFocus An object loses focus.
MouseDown The user presses any mouse button.
MouseUp The user releases any mouse button.

Table 2.1

 

In responding to an event, an object will perform certain tasks. Some of these tasks simply  return some value but some of these tasks comprises some actions ,such as moving an object to another location on a form, print something out , playing music and more, these actions are accomplished using methods. Writing codes usually involve using some of these methods. Some common methods are shown in Table 2.2.

Method Action
AddItem To add new item in a list or a combo box
Drag To response to the drag-and-drop action by the user
Hide To hide a form
Move To change an object's position in response to certain events
Print To display text on an object, such as a form.
SetFocus To shift focus to an object
Show To display a form

Table 2.2

We shall learn more about properties, events and methods and how to use them in writing code in later lessons. Now we shall concentrate on building our first application.

 

 

2.2 Creating Your First Application

In this section, we will not go into the technical aspects of Visual Basic programming yet, what you need to do is just try out the examples below to see what does a VB6 program looks like and what can it do.

Example 2.2.1 is a simple program. First of all, you have to launch Microsoft Visual Basic 6 and select Visual Basic 6  Standard EXE project. In the Visual Basic 6 Integrated Development Environment (IDE), a default form with the name Form1 will be available for you to work on your new project. Now, double click on Form1, the source code window for Form1 as shown in figure 2.1 will appear. The top of the source code window consists of a list of objects and their associated events or procedures. In figure 2.1, the object displayed is Form and the associated event is Load.

Figure 2.1 Source Code Window

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 events associated with the object will be displayed, as shown in figure 2.3. Some of the events associated with the object Form1 are Load, Activate, Click, DblClick (which means Double-Click) , DragDrop, keyPress and more. Each object has its own set of events, as we have discussed earlier . You can always select an object and write codes for any of its event in order to perform certain tasks.

 

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 surprise that nothing shown up .In order to display the output of the program, you have to add the Form1.show statement like in Example 2.2.1  or you can just use Form_Activate ( )  event procedure as shown in example 2.2.2. The method Print (as we have explained earlier) 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. We shall learn how to print text to a printer in lesson 39.

 You can also perform arithmetic calculations as shown in example 2.2.2. Visual Basic 6 uses * to denote the multiplication operator and / to denote the division operator. The output is shown in figure 2.3, where the results are arranged vertically.

 

 

 

 

Figure 2.2: List of Objects

 

 

 

Figure 2.3: List of Procedures

 

 

Example 2.2.1

Private Sub Form_Load ( )

Form1.show

Print “Welcome to Visual Basic tutorial”

End Sub

*Show is the method that displays Form1

Figure 2.4 : The output of example 2.1.1

 

Example 2.2.2

Private Sub Form_Activate ( )

Print 20 + 10
Print 20 - 10
Print 20 * 10
Print 20 / 10

End Sub

 

 

Figure 2.5: The output of 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.2.3


Private Sub

A = Tom
B = “likes"
C = “to"
D = “eat"
E = “burger"
Print A + B + C + D + E

End Sub

Example 2.2.4

Private Sub

A = Tom
B = “likes"
C = “to"
D = “eat"
E = “burger"
Print A & B & C & D & E

End Sub


The Output of Example 2.2.3 and Example 2.2.4 is as shown in Figure 2.7.

 

 

 

  Bookmark and Share

 

<Previous Lesson>  <<Home>> <Next Lesson>

Copyright ® 2008 Dr.Liew Voon Kiong . All rights reserved |Contact: admin@vbtutor.net

[Privacy Policy]