Lab 8: Writing
and Running Your First
Visual Basic Program
Spring 2001
Reading to be done prior
to Lab 8:
·
Chapter 2 of Computer
Programming Fundamentals with Applications in Visual Basic 6.0 (p. 27-50)
Recommended Reading:
·
Chapter 3 of Computer
Programming Fundamentals with Applications in Visual Basic 6.0 (p. 59-67)
Introduction:
The purpose of this lab is to introduce you to the VB Integrated Design Environment (IDE) and to create a simple VB project. The IDE is the programming environment where you create applications. A VB project is the collection of files created when you are working on an application. Your TA will go through this environment with you in lab.
You will have time to start working on this exercise in lab on Wednesday/Thursday, April 18/19. Your objective is to demonstrate your program to your TA at the end of lab.
Objectives:
TO DO:
Menu Bar Tool Bar ToolBox Project Window Properties Window Form Window
· Each time you begin a project, the default name, Project1, appears at the top of the form. To change the name, choose Project> Project1 Properties, and then type in your name as the new project name.
· When you are working with objects in VB, it is important to realize that there are lots of properties associated with them. Even the form on which you design your application is an object (and has over 50 properties that can be set). To change the form name, click on the Properties window (or select F4) to bring it into focus. Name is the very first property. Select this property field and type in “First”.
· A Text Box
· A Label
· A Picture Box
·
(4) Command Buttons
To add each object, click once on the tool in the tool box. Then go to the form and hold down the right mouse button as you “draw” you object onto the form.
·
Name the Text Box txtATextBox and change the
caption/text to: This is a text box
· Name the Label lblALabel and change the caption to: Dude, Where’s my label?
· Name the Picture Box picMyPictureBox. Notice that the picture box does not have a caption property.
· Name the first Command Button cmdClick and change the caption to: This is what you click on!
· Name the second Command Button cmdRed and change the caption to: Red!
· Name the third Command Button cmdBlue and change the caption to: Blue!
· Name the fourth Command Button cmdVisible and change the caption to: Visible!
· Make the First form purple
· Make the cmdClick button pink (NOTE: You must first change the Style to Graphical)
· Make the cmdRed button red (NOTE: You must first change the Style to Graphical)
· Make the cmdBlue button blue (NOTE: You must first change the Style to Graphical)
· Make the cmdVisible button gray (NOTE: You must first change the Style to Graphical)
· Make the lblALabel label yellow
· Make the picMyPictureBox picture box violet
· Click on the Run icon on the Tool Bar, or go to Run>Start… on the menu bar.
· Try clicking the cmdClick button. Notice that you can click the button, but nothing happens. That is because we have not added any code into our program so that something will happen when a user clicks the button. You will learn more about the event procedures that are used to control a program in the next lecture and lab.
· Since you already named our form First, you can just click OK to save it.
· You have named your project with your name, that should come up in the window. Click OK to save it.
· Your entire project, consisting of the form and the project space, is now saved.
**********************************************************
You will have 2 files associated with your project. One for the form and one for the project.
Always make sure that you send both of these when FTP’ing or using electronic turn-in.
***********************************************************
Creating ThisTime– a
digital clock with the date on the window bar:
This Time. This is a simple digital clock program with the date on the window bar. Your goal in writing the digital clock program is to create a simple VB6 program. The essential features are:
· Change the form name.
· Change the caption on the window bar.
· Place and use a label control.
· Place and use a timer control.
· Customize the window to be attractive.
· Save the project and form files.
Each of these features will be discussed below. The ThisTime application has the following window interface:
The VB environment allows you to change the properties for each object in the properties window. However, if you just use the properties window to make those changes (i.e. changing the background color, etc.), your program won’t be very exciting. We can attach code to a particular object event so that at run-time (when the program is executed) by a user, it will react in pre-coordinated ways.
As stated above, there are many
properties and events associated with objects in VB. One of the events that occur whenever someone opens up a form and
runs it is the FORM LOAD event.
· File>New Project…(your first project, that you have saved, will now be closed)
· Double click on Standard EXE
Private Sub Form_Load()
frmClock.Caption = Date
End Sub
Date is a
built-in global variant object that returns the date as set on the computer in
the form mm/dd/yy. When the form is loaded, the current date will appear on the
window bar.
Attaching code to an object:
Your code window will look similar to the above graphic. The drop down menu on the left allows you to select the object you wish to work with (your form, etc) and the objects will be listed by their Name property EXCEPT for the form object, which will always be identified as Form.
The drop down menu on the right is the list of events and procedures that can be associated with the various objects in your program.
Notice that once you select each of these options from the list, the following text appears. This is standard in VB. By default, when you select an object and select an event, the first and last lines of code that you need to have for that event are added in for you. You will type in all relevant code between those lines. Now, add in the code that you need from step 17.
This will create a file called ThisTime.vbp with a
multicolored project icon to go with it, and ThisTime.vbw
with the standard window icon. You should also save the form, which will
produce the file, frmClock.frm
with the form.
Private Sub tmrTime_Timer()
lblTime.Caption = Time
End Sub
REMEMBER!!!! VB adds in the start and
end lines of code for the event, so you should only have to type one line of
code!!!
· Change the form’s background color.
· Shrink the window so that it nicely frames the time symbols.
· Change the background color of the label to match the background of the window.
· Change the color of the font of the label (ForeDolor Property) to be attractive and to contrast nicely with the window’s background.
· Set the starting position property of the form to be the center of the screen. (Use the Form Window)