1 / 18

3.2 VB.NET Events

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.

cole
Download Presentation

3.2 VB.NET Events

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 3.2 VB.NET Events • An Event Procedure • Properties and Event Procedures of the Form • Tab Order of Controls • Exercises

  2. 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

  3. 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.

  4. 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

  5. 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

  6. Control Name Prefixes Note: Can be named anything but this is a convention Ensure your naming is intuitive!

  7. 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

  8. 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

  9. 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

  10. Program Region

  11. IntelliSense Automatically pops up to give the programmer help. Demo: wrong spelling for a control

  12. 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

  13. Event Demo Private Sub txtBox_MouseHover …. txtBox.BackColor = Color.AliceBlue End Sub Private Sub txtBox_MouseLeave …. txtBox.BackColor = Color.White End Sub

  14. Running Code • From menu – Debug – Run • Or F5 • Be careful – code is automatically saved when run

  15. Assigning properties in code • The following won't work: Form1.Text = "Demonstration" • The form is referred to by the keyword Me. Me.Text = "Demonstration"

  16. 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

  17. 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

  18. 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!

More Related