VB Objects & Events (Exercises)
School of BusinessEastern Illinois University
© Abdou Illia, Spring 2003
(Week 3, Friday 1/31/2003)
2Objects (Controls)
Pointer
Label
Frame
Check Box
Combo Box
Horizontal Scroll Bar
Timer
Directory List Box
Shapes
Image Box
Object Linking Embedding
Picture Box
Text Box
Command Button
Option Button
List Box
Vertical Scroll Bar
Drive List Box
File List Box
Lines
Data Tool
The Toolbox
Command buttons
Text box Picture box
Label
3Objects and Event [Sub]procedures
Name: cmdExitCaption: &Exit
Name: cmdFontSizeCaption: Change Font &Size
Private Sub cmdFontSize_Click() txtName.Font.Size = 18End Sub
Event procedure
Private Sub cmdBold_Click() txtName.Font.Bold = TrueEnd Sub
Event procedure
Private Sub cmdExit_Click() EndEnd Sub
Event procedure
4Event [Sub]procedures’ syntax
[Private | Public] Sub ProcedureName_Event ( ) StatementsEnd Sub
Where:Public means that the Subprocedure is accessible to all otherprocedures in the application
Private means that the Subprocedure is only accessible to allother procedures related to the form
ProcedureName is usually the name of the object associatedto an event
Event is the name of the event that might happen
Statements are the instructions to be executed at run time
5Statements
All statements on slide # 3 are Assignment statements
Assignment statements assign values to Objects’ properties
Example
txtName.Font.Size = 18 Object Property Assigned value
6Assignment Statements syntax
ObjectName.Property[.SubProperty] = Value
Where:ObjectName is the name of the object which property will be assigned a value.
Property is the object’s property which value will be assigned
SubProperty: In some case, properties may have subproperties. Subproperty refer to a particular subproperty.
Value. Also refered to as Setting, Value is the value to be assigned to the property.
7Object, Properties, Events
Each object has its related:– Properties (characteristics like name, color, size, etc.)– Events (actions it respond to)
Different Properties and Events for objects– But all objects have the Name property
Should know common properties and common events for usual objects
8Common properties & eventsObject: Command buttonCommon Properties
- Caption (Text that appear on the button)- BackColor (Color of the background)- Default (True or False)- Enabled (Can respond to User events: True or False)- Font (Font.Name, Font.Size, Font.Bold, Font.Italic..)- Visible (Make object appear or disappear: True or False)
Common Events- Click- GotFocus- LostFocus
9Common properties & eventsObject: Text boxCommon Properties
- Alignment (0-Left, 1-Right, 2-Center)- BackColor (Color of the background)- BorderStyle (Style of the border: 0 or 1)- Enabled (Can respond to User events: True or False)- Font (Font.Name, Font.Size, Font.Bold, Font.Italic..)- ForeColor (Color of the foreground)- Text (Text to be displayed)- Visible (Make object appear or disappear: True or False)
Common Events- GotFocus - Change - KeyPress- LostFocus - Click - KeyUp
10Common properties & eventsObject: Picture box
Common Properties- BackColor (Color of the background)- BorderStyle (Style of the border: 0 or 1)- Enabled (Can respond to User events: True or False)- ForeColor (Color of the foreground)- Picture (specification of a file)- Visible (Make object appear or disappear: True or False)
Common Events- GotFocus, LostFocus, Change, Click, DblClick
Common Methods: - Print - Cls
11Common properties & events
Object: Label
Common Properties- Alignment (0-Left, 1-Right, 2-Center)- Caption (Text that appear on the button)- BackColor (Color of the background)- BorderStyle (Style of the border: 0 or 1)- Font (Font.Name, Font.Size, Font.Bold, Font.Italic..)
Common EventsNote: We usually don’t associate events procedures to Labels even if it is possible to do so.
12Setting colors
At design time– Colors are selected from the palette (in Properties Win.)
When writing code– 8 most common colors can be assigned with the following
color constants:
vbBlack vbRed vbGreen vbYellow
vbBlue vbMagneta vbCyan vbWhite
Examples: txtName.ForeColor = vbGreen
picOutput.BackColor = vbWhite
13Assigning Font settings
At design time– Font settings are assigned from the Properties window
When writing code– Font settings can be assigned using statements like:
txtBox.Font.Size = 18
lblOne.Font.Italic = True
txtBox.Font.Name = “Courier”
txtBox.Font.Underline = True
14Exercises 19-32, page 68
Write a line (or lines) of code to carry out the following:
Display "The stuff that dreams are made of." in red letters in txtBoxAnswer:
Display "Life is like a box of chocolates." in Courier font in txtBoxAnswer:
15Exercises
Delete the content of txtBoxAnswer:
Delete the content of lblTwoAnswer:
16Exercises
Make lblTwo disappearAnswer:
Remove the border from lblTwoAnswer:
Give picBox a blue background
Answer:
17Exercises
Place a bold red "Hello" in lblTwo
Answer:
18Exercises
Place a bold italic "Hello" in txtBoxAnswer:
Make picBox disappearAnswer:
19Exercises
Give the focus to cmdButtomAnswer:
Remove the border from picBoxAnswer:
20Exercises
Place a border around lblTwo and center its contentAnswer:
Give the focus to txtBoxTwoAnswer:
21Problem 1
Consider the following interface and property settings
Object Property Setting
frmEx1 Caption Colorful Text
lblBack Caption Background
cmdRed Caption &Red
cmdBlue Caption &Blue
txtShow Text Beautiful Day
Multiline True
Alignment 2 –Center
lblFore Caption Foreground
cmdWhite Caption &White
cmdYellow Caption &Yellow
Write the code to carry out the following task: Pressing the command buttons alter the background and foreground colors in the textbox.
22Problem 2
Consider the following interface and property settings
Object Property Setting
frmEx2 Caption Sayings
txtQuote Text [Blank]
txtLife Text Life
txtFuture Text Future
txtTruth Text Truth
Write the code to carry out the following task: When you click on one of the three small text boxes at the bottom of the form, an appropriate saying is displayed in the large text box. Use the sayings “I like life, it’s something to do.”; “The future isn’t what it used to be”; and “Tell the truth and run”.
23Problem 3
Consider the following interface and property settings
Object Property Setting
frmEx3 Caption Fonts
txtShow Text [Blank]
cmdCour Caption Courier
cmdSerif Caption MS Serif
cmdWing Caption Wingdings
Write the code to carry out the following task: After the user type something into the text box, the user can change the font by clicking on one of the command buttons.
Other Exercise
(To do later)
25Problem 4: Payroll problem 2
Based on the Program Flowchart and the Pseudocode that follow (see next two slides), create the Visual Basic application for the following payroll problem:
Compute the pay for an employee, assuming that name, Social Security Number, hours worked, and hourly rate are input. The output will be the name, Social Security Number, and pay of the employee. Regular pay will be computed as hours (up through 40) times rates, and overtime pay will be computed at time and half (1.5 times hours times rate) for all the hours worked over 40.
26Problem 4 (Program Flowchart)
READ
NAME, SSN, HOURS, RATE
WRITE
NAME, SSN, PAY
HOURS > 40
?
PAY = REGPAY + OTPAY
PAY =
HOURS * RATE
START
STOP
YESNO
OTPAY = (HOUR – 40) * (1.5 * RATE)
REGPAY = 40 * RATE
27Problem 4 (Pseudocode)
Start
Read NAME, SSN, HOURS, RATE
IF HOURS > 40 THEN
REGPAY = 40 * RATE
OTPAY = (HOURS – 40) * (1.5 * RATE)
PAY = REGPAY + OTPAY
ELSE
PAY = HOURS * RATE
ENDIF
Write NAME, SSN, PAY
Stop
28Problem 4 (VB solution)