3.2 vb.net events an event procedure properties and event procedures of the form tab order of...
TRANSCRIPT
3.2 VB.NET Events
• An Event Procedure
• Properties and Event Procedures of the Form
• Tab Order of Controls
• Exercises
An Event Procedure
• An event is an action, such as the user clicking on a button
• Usually, nothing happens until the user does something and generates an event
Demo – Sales Order app with no code
The three steps in creating a VB.NET program:
1. Create the interface; that is, generate, position, and size the objects.
2. Set properties; that is, configure the appearance of the objects.
3. Write the code that executes when events occur.
Changing Properties
• Properties can be changed in code with the following:
controlName.property = setting
• This is an assignment statement
txtBox.ForeColor = Color.Red
Note: Right side is always assigned to left side
Demo: blank sol’n
Object Properties
• Some are values, some are string, some are boolean (true or false)
• Strings always go in quotes!txtBox.text = “Enter a phrase”
• Boolean and values do not have quotes
Control Name Prefixes
Control Prefix Example button btn btnComputeTotal label lbl lblInstructions list box lst lstOutput text box txt txtAddress
Note: Can be named anything but this is a convention
Ensure your naming is intuitive!
Most Common Errors
• Misspelling the name of a control in code
• VB editor will help you!
• Renaming controls does not update in the body of an event procedure
Event ProceduresPrivate Sub objectName_event(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles objectName.event
Shown in the book as:
Private Sub objectName_event(…) Handles objectName.event
Each object has default event procedure – double click ityou are unsure – try a label, textbox, button and listbox
Structure of an Event Procedure
Private Sub objectName_event(...)
Handles objectName.event
statements
End Sub
Sometimes there are multiple actions that can trigger an event – e.g. Textbox.enter event
Program Region
IntelliSense
Automatically pops up to give the programmer help.
Demo: wrong spelling for a control
Code for WalkthroughPrivate Sub txtFirst_TextChanged(...) Handles txtFirst.TextChanged txtFirst.ForeColor = Color.BlueEnd Sub
Private Sub btnRed_Click(...) Handles btnRed.Click txtFirst.ForeColor = Color.RedEnd Sub
Private Sub txtFirst_Leave(...) Handles txtFirst.Leave txtFirst.ForeColor = Color.BlackEnd Sub
Event Demo
Private Sub txtBox_MouseHover ….
txtBox.BackColor = Color.AliceBlue
End Sub
Private Sub txtBox_MouseLeave ….
txtBox.BackColor = Color.White
End Sub
Running Code
• From menu – Debug – Run
• Or F5
• Be careful – code is automatically saved when run
Assigning properties in code
• The following won't work:
Form1.Text = "Demonstration"
• The form is referred to by the keyword Me.
Me.Text = "Demonstration"
The Declaration Statement of an Event Procedure
• A declaration statement for an event procedure:Private Sub btnOne_Click(...) Handles btnOne.Click
• The name can be changed at will. For examplePrivate Sub ButtonPushed(...) Handles btnOne.Click
• Handling more than one event:Private Sub ButtonPushed(...) Handles btnOne.Click, btnTwo.Click
Tab Order of Controls
• Determines which control gets the focus when you hit the Tab key
• Order is initially established by the order controls are placed on the form
• Controlled by tabindex property
• Demo with Sales Order form
Exercises p. 69 to 74
Solutions to odd numbers are in the back of text book – try some on your own!
Do #37 together & #38 aloneHomework - #46 – page 74