Lesson III - Visual Basic Programming and Common Controls
In this lesson we shall discuss the programming method and commonly used basic controls. Visual Basic is just an improved version of the BASIC language that has "visual" features like controls and forms to function better in a Windows environment. To use Visual Basic you must first have good BASIC programming skills. It is recommended that you learn QBASIC or BASIC before you proceed to learn Visual Basic - in fact, it is essential that you do so. The "visual" part consists of forms and controls which serve as the user interface. Without code, they are just some pretty eye candy that do nothing at all. Each form, control and module must be assigned with it's own code in order to function. This code is written in a slightly different version of BASIC. The most important change from the standard QBASIC/BASIC is the use and reference of controls. The controls and their behavior are accessed by using code. Each object (ie. a control) has it's own properties that define it's behavior. These properties can be changed by code. The syntax for referring to an object and it's properties from within the code is as follows : <objectname.type.property>. For example if I wanted to access the "text" property of a "Text Box" (a type of control) named "Test1" I would type : Test1.text . In this case I need not specify the type because there is only one object with the name "Test1". The code for objects are accessed by double clicking on them. Now that you know how to refer to objects in your code we shall proceed to explore the common controls in the ToolBox. Out of the many controls in the ToolBox, the most commonly used ones Label, Command Button, Text Box and Image Box. To find out the names of the commands, just move your cursor over the buttons and wait for a few seconds and the name of the control will appear.

The Command Button
The command button is the most used control that is found in almost all Windows applications. It takes the form of a gray square or rectangle take looks depressed when clicked. It is used to carry out the associated code when clicked. To create a command button, click the control button then drag your cursor on the form. Release the mouse button when you have created the desired size for the command button. A command button with the text command1 will appear. You can resize and reposition this button. To access the properties of this control hit "F4" on the keyboard to bring up the properties window. Scroll up and down in the properties window. You will see a property called "Caption". You can change properties by clicking on the box to the right of the property. Modify the Caption property. Type in text in the box to the right of the Caption property. You will notice that the text in the command button changes accordingly. However this text is just for show and user interface but it serves no programming purposes. The real name of the command button is access via the "Name" property. Change this property to define the control's name. This name will be the object name that will be used for reference in the code.

The Label Control
The label control is just what it says - a label. It is basically a box that contains text. To create a label click the label button and then drag your cursor on the form. A label box will be created. It can be resized and repositioned. As with the command button, hit "F4" to access the property window. You will notice that it has different properties from the command button. All controls have properties that are unique and also properties that are similar (ie. Caption and Name). In this case, the Caption property defines the contents of the label. This can be changed by using code. The text on the label cannot be directly changed by the user.

The Text Box
The text box produces a similar effect as a label except the text on it can be directly manipulated by the user. In the properties window, the predefined text is not controlled by the Caption property. Instead it is controlled by the Text property. To create a text box follow the same procedure as when creating a label. It can also be resized and repositioned. The text box is used to obtain input from the user in the form of text. Text boxes also contain code for it to operate.

The Image Box
The image box allows you to insert graphics in bitmap form in your program. It is created in the same manner as the previous controls and can be resized and repositioned after creation. VB4 only supports BMP (Windows Bitmap) files whereas VB5 supports many types from GIFs to JPGs. Hit "F4" to access the properties box. Look for the "Picture" property. This property defines the image to be placed within the image box. Click on the box next to the property to open a "file open" dialog box. Select the graphic files from here. The image will be placed within the image box. This graphic will be stored in an *.frx file whereas the code will be stored in a *.frm file. The *.frx file can grow greatly in size with the addition of many graphics. Therefore, the file in the Picture property can also be assign by code as the Image control also has it's own code.

This concludes our first look at Visual Basic and some of it programming techniques and simple controls. In the next lesson we will construct and discuss our first program using your knowledge of these simple controls!

Back to Learning Visual Basic.

If you have any comments or questions please write to CrydoVax (Varian Qua)
All graphics, html and text on this page is copyright of Varian Qua 1997

© 1997 varian@tm.net.my
Find out how you can contribute.
Vote for this page here.
Back to 3D Guru.
Back to 3D Studio.
Back to 3D Basix
Back to SiliconValley/Horizon.


This page hosted by GeoCities Get your own Free Home Page
1