2.1 Creating Your First ApplicationIn 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 how does in VB program look like: Example 2.1.1 is a simple program. First of all, you have to launch Microsoft Visual Basic 6. Normally, a default form with the name Form1 will be available for you to start 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 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 write codes for any of its procedure in order to perform certain tasks.
|
Example 2.1.1 Private Sub Form_Load ( ) Form1.show Print “Welcome to Visual Basic tutorial” End Sub
| Figure 2.4 : The output of example 2.1.1
|
Example 2.1.2 Private Sub Form_Activate ( ) 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)
|
|
The Output of Example 2.1.4(a) &(b) is as shown in Figure 2.7.
2.2 Steps in Building a Visual Basic Application
Step 1 : Design the interface
You need to design an interface that is user-friendly and easy to navigate. Give clear instructions by typing suitable captions for the command buttons and labels. If you design the program for fun and games, it has to be reasonably attractive. You can look at the interface of my Star War game, as shown below:
Step 2 : Set properties of the controls (Objects)
Each Visual Basic control has its own properties. For example, a textbox has font color property, font size property, text alignment property and more. Setting properties will determine how a control looks and behaves. Therefore, we need to set suitable properties for a control so that the program will run as intended to be. You will more about properties in the next chapter
Step 3 : Write the event procedure
The final and the most difficult step is to write the event procedure. An event procedure consists of VB code that control how a VB event executes. We have to write code for any possible event trigger by the user, like clicking the button or pressing a key on the keyboard. Although writing code is not easy, fortunately Visual Basic programming language is the easiest to master amongst all the programming languages. It is English-like so if you know English you should have no problem in mastering the language. We will guide you step by step on writing the VB code in the coming chapters.