Part 1 - Housekeeping / Overhead
Start Visual Basic
From the New Project Dialog, select Standard EXE and click on Open
This is the default selectionYou are now looking at the VB Environment
Note the Toolbox, Propery Windows, and the Form WindowIn the Properties Window for the Form, change two properties:
Name: frmMain Caption: Your Last Name - Example 1 You will see that the Title Bar of the Form now has a new Caption
In the Properties Window for the Project, change the Name property to Your Last Name with E01 appended
You will perform these steps with EVERY new project.
Part 2 - Building the Example Application
Add three command buttons and two labels to the form as follows:
Modify the properties of each control as follows:
Command Buttons | |||
cmdHello | Caption: Hello | ||
cmdClear | Caption: Clear | ||
cmdExit | Caption: Exit | ||
Labels | |||
lblMessage | Border Style: Fixed Single | ||
Font Size: 12 | |||
Caption: "Click a Button" | |||
Label1 | Take the Default for the name | ||
Font Size: 12 | |||
Font Style: Bold | |||
Caption: Message |
You have completed the User Interface
Run the program: Click on Run | Start With Full Compile
The program should run fine but it really doesn't do anything
except display the Uesr Interface.
Now you get to write the BASIC code that will handle the Command Button Click Events.
Terminate the program by clicking on the small 'x' in the upper right hand corner of the program window.
![]() |
![]() |
In this program, when the user clicks on the "Hello"
button, the message should say "Hello". When the user clicks on the
"Clear" button, the message should be blank. When the user clicks on the
"Exit" button, the program should terminate.
Each of these three objects triggers a specific reaction from your program.
Click on the Code Window button in the Project Properties window.
The code windows is where you will write your Event Handlers
Begin with a Comment Block as follows
'
' Name
' Class and Meeting Time
' Brief Description of this Program
'
A single quote indicates a comment in Visual Basic
Below the Comment Block type:
Option Explicit
This directive requires you to declare all variables that you use rather than letting BASIC declare them for you. This is important because you will get very confused if you declare a variable with one name, and then refer to it in your program with a different name. Option Explicit will cause BASIC to generate a syntax error.
Click on the List of Objects and select cmdHello. This will result in the following:
Private Sub cmdHello_Click()
End Sub
This subroutine will be executed whenever the user clicks on the "Hello" button.
The requirements of this program tell us you that when the user clicks on the Hello Button, the Message should be changed to read "Hello". In order to do this, you must assign the string "Hello!" to the Caption property of lblMessage.
lblMessage.Caption = "Hello!"
When the user clicks on the "Clear", the message should be changed to blank, and when the user clicks on the "Exit" button, the program should terminate. Use the graphic of the code window to complete the example.
There are a couple of features of Visual Basic that you may have noticed. One is the "auto fill in" feature. In the example below, after you type the object name folowed by a dot (the separator between the object and its properties), VB will provide you with a list of valid properties.
Also note that if I have an object names "lblMessage" and I type in "lblmessage" (note the case difference), VB will change the case to match the object name. You will develope "an eye" for this behaviour and will notice that if the case DOES NOT change, then you have made a mistake as you have refered to an object that does not exist. DO NOT type out the names of Object Properties or Object Event Handlers.
Let Visual Basic Do It
It's also not a good idea to "argue" with VB. If the case does not change, or if the property list does not appear, or the Event Handler is not listed, step back and try to determine what YOU have done wrong. VB will win these disagreements EVERY time.
One more item worth noting:
You can Double Click on the object from the User Interface and the Code Window will open, and at the same time create a Subroutine for the default Event Handler for the selected object. So if you Double Click on a Command Button, Visual Basic will open the Code Window and create a Command Button Click Event Handler for your selected button.
Run the program and see if it reacts to your mouse clicks the way you expect that it should. When you are satisfied, you can create an Executable version of your program.
Click on File | Make .EXE
Use the Dialog to create the .EXE in your Project Folder
![]() |
After the .EXE has been created, close VB, open
Windows Explorer, and navagate to your project directory. Double Click on your Program File and you will see that your program will run outside the VB environment.
If you were to copy the .EXE to your diskette and later copy it to another workstation, your program may or may not run. The target workstation must have the VB runtime modules installed.
|
This completes the first example