Lab
8: Writing and Running Your First
Visual
Basic Program
Spring
2002
1. Create
a New Project and Form
2. Add
Objects to the Form and Name Them
3. Manipulate
Object Properties
4. Saving
the Project and Form
6. Attaching
code to an object
7. Adding
a Clock/Timer Object and Using the Timer Event
·
Chapter
2 of Computer Programming Fundamentals with Applications in Visual Basic 6.0
(p. 27-50)
·
Chapter
3 of Computer Programming Fundamentals with Applications in Visual Basic 6.0
(p. 59-67)
·
OR
Chapter 3: Performing Operations and Storing the
results from The Visual Basic Coach.
This is on e-Reserve at the library:
https://eres.lib.washington.edu/coursepage.asp?cid=1113&page=01
We
will be placing readings for labs on electronic reserve. When they are available, a link will be
placed on the lab pages as well as on the
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.
A. Open Windows Explorer. Create a
folder called Lab8_yourname on the Desktop
B. Open up Visual Basic 6.
Start>Program Files>Microsoft Visual Studio 6.0>Microsoft Visual
Basic 6.0
When you start VB you will be presented with a “New Project” dialog box. Select
the default project type, “Standard EXE”.
The
IDE typically contains the menu bar, the tool bar, the toolbox, and three
other tool windows: Form, Project, and Properties.
The Visual Basic Integrated
Development Environment (IDE)
Menu Bar Tool Bar ToolBox Project Window Properties Window Form Window
To change the form name, click on the Properties window (or press F4) to bring it into focus. Name is the very first property. Select this property field and type in “frmFirst”.
· A Text Box
· A Label
·
(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 left mouse button as you “draw” the object onto the form.
·
Name the Text Box: txtATextBox
Change the Text to: This is a text box
·
Name the Label lblALabel:
Change the Caption
to: This is a label
·
Name the first Command
Button: cmdClick
Change the Caption to: This is
what you click!
·
Name the second Command
Button: cmdGreen
Change the Caption to: Green!
·
Name the third Command
Button: cmdYellow
Change the Caption to: Yellow!
·
Name the fourth Command
Button: cmdVisible
Change the Caption to: Visible!
·
Make the frmFirst
form blue
·
Make the cmdClick
button purple (NOTE: You must first
change the Style property to Graphical)
·
Make the cmdGreen
button green (NOTE: You must first
change the Style property to Graphical)
·
Make the cmdYellow
button yellow (NOTE: You must first
change the Style property to Graphical)
·
Make the cmdVisible
button lilac (NOTE: You must first
change the Style property to Graphical)
· Make the lblALabel label red
· 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 handler procedures that are used to control a program in the next lab.
·
Since you already named our form frmFirst, you can just click OK to save it.
·
You have named your project with
your name. Your name 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. The extension on a form file is .frm and the extension for a project file is .vbp.
Now that you have had time to get familiar with forms and
other objects in the Visual Basic IDE, we’ll create a little program that uses
a bit of code to do display the date and time in our form.
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 with the digital clock 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 ExactTime
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. 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. Select yes to save changes)
· Double click on Standard EXE
· Select a better font, and enlarge it to approximately 20 points.
Private Sub Form_Load()
frmClock.Caption = Date
End Sub
|
ExactTime for the project
frmClock for the form
You are prompted to save the form
first, and then the project. When you
have saved both, two files are created: ExactTime.vbp and frmClock.frm.
Private Sub tmrTime_Timer()
lblTime.Caption = Time
End Sub
Time is like Date, a function that returns a variant indicating the
computer system time in HH:MM:SS format. The
timer "goes off" every second. That event causes the procedure
"tmrTime" to be
called. It changes the caption of the label to the current time. Whenever
anything on the form is changed, the window is "repainted," i.e.
redisplayed. Since most of the information is unchanged and remains in a
fixed position, the clock appears to be ticking.
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!!!
What happens is this:
· 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 (ForeColor 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 in the lower right of your screen)
A. Write an explanation, as best you can, of what an event is in the real
world and how events are used in the computer world.
B. What is the difference between the Name of an object and its caption?
C. Why are names so important when programming?