Properties
Characteristics
Properties are a number of variables associated with an object
Each object has a different list of properties
Some properties are common to most objects
Visible
Some properties are specific to only one type of object
Max, Min in scroll bars
Can have different types of values:
Text (caption)
Numeric (Max, Min)
Boolean-True/False (Visible)
Properties are used to:
Refine an object's appearance (BackColor)
Refine an object's behavior (Enabled)
Set up the object so it will do what the programmer wants
(design - time)
Change the characteristics of the property while the program
is running (run - time)
Properties can be changed at:
Design time
As program is being built
Uses properties box
All properties will be set this way when the program starts
execution
Run time
While the program is running
Done with code
Allows properties to be changed 'on the fly'
Allows response to environmental (EG user) input
Example: Myfirst used--
Design time to set up BackColor in picture boxes
Run time to set up BackColor in Form
Methods
Characteristics
A special kind of command attached to specific objects.
Works only with the object it is attached to.
Some objects (Check boxes) have few methods
Others (Data controls) have many
Usually found by searching the online help
Activated only in run time.
Relatively limited use
Syntax:
Object.Method
Example
Move event
Used to move an object
Syntax: Object.Move X,Y, Height,Width
X,Y are Coordinates object will be moved to
Height, Width are size of object
Common Methods:
Hide, Show used with forms
Additem used with listboxes, grids
Print, EndDoc to print to printer
Extensive methods for data control object
" " graphics on form or picture box
Events
Characteristics:
The stimuli an object can recognize
Listed in the Proc box of the object's code window
Some events are nearly universal (EG Click)
Some are object - specific (EG Timer)
Can be activated by user (EG Click)
" " environment (EG Load, Timer)
Event - Driven program design
Most code in VB is related to an object - event relationship
Example: Picture1_Click in last week's program
Read "when the user CLICKS on the PICTURE1 control,
this will happen:"
The user or environment activates events in objects,
which then activate code
Very different than earlier types of program organization techniques
Common Events:
Click, Double - Click
Load, Unload on forms
Change on controls that get user input
Paint on controls that contain graphical information
Getting Help
Concepts
Visual Basic is a complex, growing language with many subtleties
Traditional reference books are cumbersome
Hypertext online help
Uses Windows Help engine
Techniques for getting help
Help Menu in VB
Contents submenu is good if you need general information
(a list of events, commands, etc)
Search for Help on... is much more flexible
Type in whatever you want help on.
A list of similar topics will appear.
Choose the topic you want
Keyword Help
Fastest way to get help
Type or point to a keyword that you are having trouble with in a code window
Be sure the cursor is somewhere inside that word
Hit the F1 key.
If the word is a keyword and spelled correctly,
a help screen for that word will appear.
This is especially useful for checking syntax
EG you know what command you need but don't remember
the exact parameters
New Objects:
Timer
Description
Invisible at run time
Goes "off" at specified intervals
Used to replace loops (!)
Properties
Interval (in 1000ths of a second)
Enabled (True/False)
Events
Timer
It has "gone off"
New Properties:
BackColor, ForeColor
Attached to most objects
FontName
Tells which font to use in current object
Must be a valid windows font (TrueType fonts are best)
FontSize
Tells the size of the current font.
(looks best with True Type fonts)
New Functions
QBColor
Description
Gets a color using the QBasic values
Uses a number between 0 and 15
an easy way to get the most common colors
Syntax:
Object.Property = QBColor(ColorNum)
Time$
Description
Gets the Current time from the system clock, stores it as a string
Syntax
Variable or object.property = Time$
End
Ends the program (!)
Sketch:
Open Visual Basic and start with the default form. Rename it as CLOCK and change the caption to something like "{your name}'s colorful clock". Change the background color if you wish.
Add a large label to the top center area of the form. Call it LblClockFace and give it an empty caption. Set the FontName Property to Arial.
Add a timer control to the form. (It doesn't matter where.) Set the Interval property of the timer to 500 (1/2 second). Double-Click to get the code window. Add the proper code to the Timer1.Timer event. (Each time the timer activates, we want the clockface updated with the current time). Test your code.
Add a horizontal scroll bar under the clock face. Change the Max property to 15.
Add code to the change event to set the ClockFace's foreground color to the current value of the
Scrollbar. Add a label near the scrollbar to indicate this scrollbar will change foreground color.
Make a second horizontal scrollbar that is identical to the first, but have it change the background color of the clockFace. Add a label to show this as well.
Test again.
Add a vertical scroll bar to change the font size of the clock. Make the Max value 200 and the Min value 10. Make the LargeChange value equal to 10. Add code to the change event that will set the font size of the ClockFace (LblClockFace.FontSize) equal to the value of this scrollbar (VScroll1.Value)
Test again.
Add a Quit Command button and the code to make it operate.
Save project as A:VB3.MAK and the form as A:CLOCK.FRM
Challenge:
Try to guess what the following code would do when activated:
LblClockFace.Move LblClockFace.Left + 100, LblClockFace.Top
LblClockFace.Move LblClockFace.Left - 100, LblClockFace.Top
Add another command button that when clicked,
activates this code.See if you guessed right.
Return to the Syllabus.