Introduction To Programming Using Visual Basic 6
Introduction To Programming Using Visual Basic 6
The VB IDE
View Code
Menu bar
View Object
Project
Explorer
Window
Tool bar
Tool box
Project
Window
Immediate
Window
Properties
Window
View Menu
As in all Windows
applications, simply
choose View from the
menu to view your
different toolboxes,
windows, etc. or to
remove them from the
IDE.
4
Objects
Every VB app has at
least one object the
form.
Object Properties
All objects should be named. Each object has a
prefix that is standard in the VB coding world.
For example, the prefix for a form is frm. See the
VB help for a complete list.
Objects have many properties that can be accessed at
design time or at run time.
VB uses dot notation to access propertiesfor
example: lblMessage.Caption = Welcome!
Object
Property
Property
Value
Events
An event procedure is a block of code that
executes in response to an event. A user
event is an action performed by the user
such as a mouse click.
Open VB and
choose a New,
Standard Exe
program.
Hello World
2.
3. Type in the
Caption as
shown. Note
it change in
the title bar.
Hello World
4.
Label Object
6.
7.
8.
Add a label
object and
resize it
accordingly.
Change the
name to
lblMessage.
Change the
Caption to
Welcome!
11
Font Property
9. Choose the
Font
property
and make
your font
larger and
of a
different
type.
12
Alignment Property
13
Command Button
11.
12.
Add a
command
button by
double
clicking that
object in the
toolbox.
Move it as
shown.
Name the
button
cmdDisplay
and change the
caption to
Display.
14
Done Button
Add a button
called cmdDone
and change the
caption to Done.
Note: If you choose
to Copy and
Paste a control,
you will get a
message box re:
Control Array.
Answer NO to
this.
13.
15
Time to Code
Unload Me
Adding the code Unload Me to the
cmdDone_Click event tells the computer that
when the control named cmdDone is clicked, the
form (me) will unload.
To run your program, choose Run from the menu,
press F5, or click the Run button on the toolbar.
Clicking the Done button should unload your form.
17
Display Button
The code for the Display button will make the
Caption in the label lblMessage change to
Hello, World.
15. Double click the Display button and type in
the following code:
lblMessage.Caption=Hello, World
Run your program and click the
Done button.
18
19
Colors
We can alter the look of your form
with colors and graphics.
18. Select lblMessage. Choose
the Backcolor property, then
Palette.
Select a color of your own choice.
19. Do the same for the Forecolor.
22
Colors on Buttons
You change the color of a buttons
background, but first you must set its
Style property to Graphical.
20. Choose the Display button, then change
the Backcolor and the Style property.
Note that there is no Forecolorthe font stays
black.
23
Graphics
We can also add graphics to some objects.
21. Select the Picture property of
the Form, then choose an
image. This one is at
g:\klimack\images\bonnell.
Change the file type to All
Files if nothing is showing
up.
22. Change your Done button picture property remembering to
24
change the Style to Graphical.
Message Boxes
23.
25
Comments
Adding comments in VB is done by prefixing your statement
with an apostrophe. For example:
this is a comment and will be ignored by the computer
Break Time!
27
Pizza Project
We will create the
project shown to
the left with
checkboxes,
option buttons,
textboxes,
message boxes,
command buttons
and labels.
28
1.
2.
3.
4.
txtName
lblName
txtPhone
lblPhone
30
Aligning
You can line up objects on your form evenly
by selecting them (holding down the shift
key allows multiple selections) and then
choosing Format, Align, etc. Notice the
other options under the Format menu.
5. Line up your labels and text boxes.
6. Choose both labels and text boxes at the
same time, then select Font and make
them all 10 or 12 point.
31
Frames
Frames are used as containers for other
controls. Usually groups of option buttons
are placed in frames.
7. Add a frame named fraSize.
8. Make the Caption Size.
32
Options on Form
34
Checkboxes
36
Time to Code!
The specs for this program state that the order
can be cancelled at any time. After order
placement, a message box will appear
asking the user to verify the order as
indicated. If it is verified another message
box will indicate this.
38
Enabling Controls
It would be appropriate for these checkboxes to be
enabled after choosing a pizza size.
17. Double click optSmall, and add this code to the
procedure.
chkPepperoni.enabled=true
Run the program. Click on Small and
Pepperoni should become enabled.
18. Enable each of the checkboxes in the
same manner.
40
Choose the
procedures
here.
41
42
Cancel Button
If the user clicks the
Cancel button, all the
info currently entered
should disappear and
the form reset to the
initial screen.
23. Get to the
cmdCancel_click
procedure and enter
the following lines of
code:
45
Canceling, continued
'clear the size options
optSmall.Value = False
optMedium.Value = False
chkSausage.Value = vbUnchecked
optLarge.Value = False
chkMushrooms.Value = vbUnchecked
chkGreenPeppers.Value = vbUnchecked
chkExtraCheese.Value = vbUnchecked
chkOnions.Value = vbUnchecked
optPickup.Value = True
46
Canceling, continued
Last of all, disable the checkboxes again and set the focus to txtName.
Break Time!
48
Global Variables
We are going to
use some Global
variables for
this program.
24. Add the lines
shown to your
program. These
variables can be
used by all the
procedures.
49
Constants
Every time the user selects or de-selects a topping, the topping price
must change. In this case, toppings are 75 cents each.
25. Constants should be declared before variables, and these are global
as well, so add them BEFORE the 2 global variables you just added.
50
Topping Price
We need to ADD to the curToppings every time a new
topping is selected, and SUBTRACT every time one is
de-selected.
26. In each chk procedure, add code similar to the
following, changing the Pepperoni to the appropriate
topping.
If chkPepperoni.value=vbChecked then
curToppings=curToppings + curToppingPrice
Else
curToppings=curToppings curToppingPrice
End if
51
Debug.Print
statements
Results of
Debug.print
53
Size Price
The size price depends upon whichever option is
selected. Each option procedure needs to have
code similar to the following:
curSize = curSmall add to
optSmall
curSize=curMedium add to
optMedium
curSize=curLarge add to optLarge
54
Total Price
We need to calculate the total due as an
addition of curSize and curToppings.
27. Declare curTotal as a global variable of
type currency.
Private curTotal as currency
in the General section.
55
strSize
In each of the procedures for the size
options, add a line to assign a string to
string size that corresponds with the size.
For example, in optSmall add:
strSize=Small
In optMedium, add strSize=Medium &
in optLarge add strSize=Large.
31.
58
59
Run your
project and
click the order
button with
some text
boxes left
blank.
60
&
concatenate
61
Toppings, continued
Delivery Method
The following code can be used to determine the appropriate
string for the method of delivery:
34. Declare a variable strDeliveryMethod as string in the Order
procedure. Then add the following to the bottom of the
procedure.
'check for delivery or pickup
If optDelivery.Value = True Then
strDeliveryMethod = "Delivery"
Else
strDeliveryMethod = "Pickup"
End If
63
64
Msgbox()
vbYes, vbNo
If the order is correct, print the form and clear
it (the same as Canceling).
If intOrderCorrect = vbYes Then
Me.PrintForm
Call cmdCancel_Click
End if
If the order is not correct, do nothing to the
program.
67
Program Complete!
68
Conclusion
Thank you for attending this workshop.
Available on-line in zipped format is:
This presentation
Hello World program source
Pizza source
Source for the simple programs included in your package
69