270 likes | 618 Views
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.
E N D
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: • Create the interface; that is, generate, position, and size the objects. • Set properties; that is, configure the appearance of the objects. • 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 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 Procedures Private SubobjectName_event(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles objectName.event Shown in the book as: Private SubobjectName_event(…) HandlesobjectName.event Each object has default event procedure – double click it you 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
IntelliSense Automatically pops up to give the programmer help. Demo: wrong spelling for a control
Code for Walkthrough Private Sub txtFirst_TextChanged(...) Handles txtFirst.TextChanged txtFirst.ForeColor = Color.Blue End Sub Private Sub btnRed_Click(...) Handles btnRed.Click txtFirst.ForeColor = Color.Red End Sub Private Sub txtFirst_Leave(...) Handles txtFirst.Leave txtFirst.ForeColor = Color.Black End 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 example Private 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 Do #37 together & #38 aloneHomework - #46 – page 74 Solutions to odd numbers are in the back of text book – try some on your own!