Download presentation
Presentation is loading. Please wait.
Published byRichard Skinner Modified over 9 years ago
1
VB Objects & Events (Exercises) School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 3, Friday 1/31/2003)
2
2 Objects (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
3
3 Objects and Event [Sub]procedures Name: cmdExit Caption: &Exit Name: cmdFontSize Caption: Change Font &Size Private Sub cmdFontSize_Click() txtName.Font.Size = 18 End Sub Event procedure Private Sub cmdBold_Click() txtName.Font.Bold = True End Sub Event procedure Private Sub cmdExit_Click() End End Sub Event procedure
4
4 Event [Sub]procedures’ syntax [Private | Public] Sub ProcedureName_Event ( ) Statements End Sub Where: Public means that the Subprocedure is accessible to all other procedures in the application Private means that the Subprocedure is only accessible to all other procedures related to the form ProcedureName is usually the name of the object associated to an event Event is the name of the event that might happen Statements are the instructions to be executed at run time
5
5 Statements n All statements on slide # 3 are Assignment statements n Assignment statements assign values to Objects’ properties Example txtName.Font.Size = 18 Object Property Assigned value
6
6 Assignment 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.
7
7 Object, Properties, Events n Each object has its related: – Properties (characteristics like name, color, size, etc.) – Events (actions it respond to) n Different Properties and Events for objects – But all objects have the Name property n Should know common properties and common events for usual objects
8
8 Common properties & events Object: Command button Common 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
9
9 Common properties & events Object: Text box Common 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
10
10 Common properties & events Object: 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
11
11 Common 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 Events Note: We usually don’t associate events procedures to Labels even if it is possible to do so.
12
12 Setting colors n At design time – Colors are selected from the palette (in Properties Win.) n When writing code – 8 most common colors can be assigned with the following color constants: vbBlackvbRedvbGreenvbYellow vbBluevbMagnetavbCyanvbWhite Examples: txtName.ForeColor = vbGreen picOutput.BackColor = vbWhite
13
13 Assigning Font settings n At design time – Font settings are assigned from the Properties window n 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
14
14 Exercises 19-32, page 68 Write a line (or lines) of code to carry out the following: n Display " The stuff that dreams are made of. " in red letters in txtBox Answer: n Display " Life is like a box of chocolates. " in Courier font in txtBox Answer:
15
15 Exercises n Delete the content of txtBox Answer: n Delete the content of lblTwo Answer:
16
16 Exercises n Make lblTwo disappear Answer: n Remove the border from lblTwo Answer: n Give picBox a blue background Answer:
17
17 Exercises n Place a bold red "Hello" in lblTwo Answer:
18
18 Exercises n Place a bold italic "Hello" in txtBox Answer: n Make picBox disappear Answer:
19
19 Exercises n Give the focus to cmdButtom Answer: n Remove the border from picBox Answer:
20
20 Exercises n Place a border around lblTwo and center its content Answer: n Give the focus to txtBoxTwo Answer:
21
21 Problem 1 n 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.
22
22 Problem 2 n 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”.
23
23 Problem 3 n 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.
24
Other Exercise (To do later)
25
25 Problem 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.
26
26 Problem 4 ( Program Flowchart ) READ NAME, SSN, HOURS, RATE WRITE NAME, SSN, PAY HOURS > 40 ? PAY = REGPAY + OTPAY PAY = HOURS * RATE START STOP YES NO OTPAY = (HOUR – 40) * (1.5 * RATE) REGPAY = 40 * RATE
27
27 Problem 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
28
28 Problem 4 (VB solution)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.