1 / 27

Fundamentals of Programming in Visual Basic (VB)

Fundamentals of Programming in Visual Basic (VB). Visual Basic Events Simple Statement. Event. An event is an action, such as the user clicking on a button Usually, nothing happens in a Visual Basic program until the user does something and generates an event.

kbarco
Download Presentation

Fundamentals of Programming in Visual Basic (VB)

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. Fundamentals of Programming in Visual Basic (VB) • Visual Basic Events • Simple Statement Chapter 3

  2. Event • An event is an action, such as the user clicking on a button • Usually, nothing happens in a Visual Basic program until the user does something and generates an event. • What happens is determined by statements. Visual Basic Events Chapter 3

  3. Assignment Statements • Assign a value to a property. General Form: source = value • A value on the right-hand side of = is assigned to the left-hand size of =. Chapter 3

  4. Sample Statements Color is a structure allowing us to specify various colors • txtBox.ForeColor = Color.Red • txtBox.Visible = True • txtBox.Text = “Hello World” General Form: controlName.property = setting Value represented by setting is stored into controlName.property Chapter 3

  5. Lab • Refers to the examples in the Lab Chapter 3

  6. Sample Form txtFirst txtSecond btnRed Chapter 3

  7. Focus • When you click on a text box, a cursor appears in the text box, and you can type into the text box. • Such a text box is said to have the focus. • If you click on another text box, the first text box loses the focus and the second text box receives the focus. Chapter 3

  8. Examples of Events This event occurs when a mouse is clicked on the button btnShow • btnShow.Click • txtBox.TextChanged • txtBox.Leave This event occurs when user changes the text of txtBox This event occurs when the input focus leaves txtBox General Form: controlName.event Chapter 3

  9. The three steps in creating a Visual Basic 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. Chapter 3

  10. Page tab Class Name box Method Name box Code Window Page tab: 1. frmDemo.vb is a code window 2. frmDemo.vb [design] is a design window (to manipulate the window appearance) Chapter 3

  11. Structure of an Event Procedure The name of the procedure Private SubobjectName_event(...) HandlesobjectName.event statements End Sub Header To specify which event will trigger this procedure. (...) is filled automatically with (ByVal sender As System.Object, ByVal e As System.EventArgs) Chapter 3

  12. Code Window Page tab Class Name box Method Name box Chapter 3

  13. Create an Outline for an Event Procedure; i.e. header and End Sub 1. Double-click on a control or 2. Use the Class Name and Method Name boxes. (We primarily use the first method.) Chapter 3

  14. Sample Form txtFirst txtSecond btnRed Double Click on txtFirst Chapter 3

  15. Code for Walkthrough Public ClassfrmDemo Private SubtxtFirst_TextChanged(...) Handles txtFirst.TextChanged End Sub End Class TextChanged event occurs when the user changes the text of a TextBox Chapter 3

  16. Code for Walkthrough Public ClassfrmDemo Private SubtxtFirst_TextChanged(...) Handles txtFirst.TextChanged txtFirst.ForeColor = Color.Blue End Sub End Class Chapter 3

  17. IntelliSense Automatically pops up to give the programmer help. Chapter 3

  18. Code Window Click tab to return to Form Designer Chapter 3

  19. Sample Form txtFirst txtSecond btnRed Double-click on btnRed Chapter 3

  20. Code for Walkthrough Public ClassfrmDemo Private SubtxtFirst_TextChanged(...) Handles txtFirst.TextChanged txtFirst.ForeColor = Color.Blue End Sub Private Sub btnRed_Click(...) Handles btnRed.Click End Sub End Class Chapter 3

  21. Code for Walkthrough Public ClassfrmDemo Private SubtxtFirst_TextChanged(...) Handles txtFirst.TextChanged txtFirst.ForeColor = Color.Blue End Sub Private Sub btnRed_Click(...) Handles btnRed.Click txtFirst.ForeColor = Color.Red End Sub End Class Chapter 3

  22. Event Procedure txtFirst.Leave • Select txtFirst from Class Name box drop-down list. • Select Leave from Method Name box drop-down list. Chapter 3

  23. Code for Walkthrough Private SubtxtFirst_Leave(...)Handles txtFirst.Leave End Sub Private SubtxtFirst_TextChanged(...) Handles txtFirst.TextChanged txtFirst.ForeColor = Color.Blue End Sub Private Sub btnRed_Click(...) Handles btnRed.Click txtFirst.ForeColor = Color.Red End Sub Chapter 3

  24. Code for Walkthrough Private SubtxtFirst_Leave(...)Handles txtFirst.Leave txtFirst.ForeColor = Color.Black End Sub Private SubtxtFirst_TextChanged(...) Handles txtFirst.TextChanged txtFirst.ForeColor = Color.Blue End Sub Private Sub btnRed_Click(...) Handles btnRed.Click txtFirst.ForeColor = Color.Red End Sub Chapter 3

  25. Header of Event Procedure Private Sub btnRed_Click(…) Handles btnRed.Click Identifies the event that triggers the procedure Name, can be changed. Private Sub Button_Press(…) Handles btnRed.Click Chapter 3

  26. Handling Multiple Events Event procedure can be invoked by two events. Private Sub Button_Click(...) HandlesbtnRed.Click, txtSecond.Leave txtFirst.ForeColor = Color.Red End Sub Chapter 3

  27. Altering Properties of the Form • The following won't work: frmDemo.Text = "Demonstration" • The form is referred to by the keyword Me. Me.Text = "Demonstration“ Chapter 3

More Related