Download presentation
Presentation is loading. Please wait.
Published bySylvia Daniel Modified over 8 years ago
1
Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement
2
Chapter 32 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
3
Chapter 33 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 =.
4
Chapter 34 Sample Statements 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 Color is a structure allowing us to specify various colors
5
Chapter 35 Lab Refers to the examples in the Lab
6
Chapter 36 Sample Form txtFirst txtSecond btnRed
7
Chapter 37 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.
8
Chapter 38 Examples of Events btnShow.Click txtBox.TextChanged txtBox.Leave General Form: controlName.event This event occurs when a mouse is clicked on the button btnShow This event occurs when user changes the text of txtBox This event occurs when the input focus leaves txtBox
9
Chapter 39 The three steps in creating a Visual Basic 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.
10
Chapter 310 Code Window Method Name box Class Name box Page tab Page tab: 1. frmDemo.vb is a code window 2. frmDemo.vb [design] is a design window (to manipulate the window appearance)
11
Chapter 311 Structure of an Event Procedure Private Sub objectName_event(...) Handles objectName.event statements End Sub Header (...) is filled automatically with (ByVal sender As System.Object, ByVal e As System.EventArgs ) To specify which event will trigger this procedure. The name of the procedure
12
Chapter 312 Code Window Method Name box Class Name box Page tab
13
Chapter 313 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.)
14
Chapter 314 Sample Form txtFirst txtSecond btnRed Double Click on txtFirst
15
Chapter 315 Code for Walkthrough Public Class frmDemo Private Sub txtFirst_TextChanged(...) Handles txtFirst.TextChanged End Sub End Class TextChanged event occurs when the user changes the text of a TextBoxTextBox
16
Chapter 316 Code for Walkthrough Public Class frmDemo Private Sub txtFirst_TextChanged(...) Handles txtFirst.TextChanged txtFirst.ForeColor = Color.Blue End Sub End Class
17
Chapter 317 IntelliSense Automatically pops up to give the programmer help.
18
Chapter 318 Code Window Click tab to return to Form Designer
19
Chapter 319 Sample Form txtFirst txtSecond btnRed Double-click on btnRed
20
Chapter 320 Code for Walkthrough Public Class frmDemo Private Sub txtFirst_TextChanged(...) Handles txtFirst.TextChanged txtFirst.ForeColor = Color.Blue End Sub Private Sub btnRed_Click(...) Handles btnRed.Click End Sub End Class
21
Chapter 321 Code for Walkthrough Public Class frmDemo 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 End Class
22
Chapter 322 Event Procedure txtFirst.Leave Select txtFirst from Class Name box drop-down list. Select Leave from Method Name box drop-down list.
23
Chapter 323 Code for Walkthrough Private Sub txtFirst_Leave(...) Handles txtFirst.Leave End Sub 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
24
Chapter 324 Code for Walkthrough Private Sub txtFirst_Leave(...) Handles txtFirst.Leave txtFirst.ForeColor = Color.Black End Sub 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
25
Chapter 325 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
26
Chapter 326 Handling Multiple Events Private Sub Button_Click(...) Handles btnRed.Click, txtSecond.Leave txtFirst.ForeColor = Color.Red End Sub Event procedure can be invoked by two events.
27
Chapter 327 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“
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.