Fundamentals of Programming in Visual Basic VB Visual

Fundamentals of Programming in Visual Basic (VB) • Visual Basic Events • Simple Statement Chapter 3 1

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 2

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

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

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

Sample Form txt. First txt. Second btn. Red Chapter 3 6

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 7

Examples of Events • btn. Show. Click • txt. Box. Text. Changed • txt. Box. Leave General Form: control. Name. event This event occurs when the input focus leaves txt. Box Chapter 3 This event occurs when a mouse is clicked on the button btn. Show This event occurs when user changes the text of txt. Box 8

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

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

Structure of an Event Procedure The name of the procedure Private Sub object. Name_event(. . . ) Header Handles object. Name. event statements End Sub To specify which event will trigger this procedure. (. . . ) is filled automatically with (By. Val sender As System. Object, By. Val e As System. Event. Args) Chapter 3 11

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

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 13

Sample Form txt. First txt. Second btn. Red Double Click on txt. First Chapter 3 14

Code for Walkthrough Public Class frm. Demo Private Sub txt. First_Text. Changed(. . . ) Handles txt. First. Text. Changed End Sub End Class Text. Changed event occurs when the user changes the text of a Text. Box Chapter 3 15

Code for Walkthrough Public Class frm. Demo Private Sub txt. First_Text. Changed(. . . ) Handles txt. First. Text. Changed txt. First. Fore. Color = Color. Blue End Sub End Class Chapter 3 16

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

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

Sample Form txt. First txt. Second btn. Red Double-click on btn. Red Chapter 3 19

Code for Walkthrough Public Class frm. Demo Private Sub txt. First_Text. Changed(. . . ) Handles txt. First. Text. Changed txt. First. Fore. Color = Color. Blue End Sub Private Sub btn. Red_Click(. . . ) Handles btn. Red. Click End Sub End Class Chapter 3 20

Code for Walkthrough Public Class frm. Demo Private Sub txt. First_Text. Changed(. . . ) Handles txt. First. Text. Changed txt. First. Fore. Color = Color. Blue End Sub Private Sub btn. Red_Click(. . . ) Handles btn. Red. Click txt. First. Fore. Color = Color. Red End Sub End Class Chapter 3 21

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

Code for Walkthrough Private Sub txt. First_Leave(. . . ) Handles txt. First. Leave End Sub Private Sub txt. First_Text. Changed(. . . ) Handles txt. First. Text. Changed txt. First. Fore. Color = Color. Blue End Sub Private Sub btn. Red_Click(. . . ) Handles btn. Red. Click txt. First. Fore. Color = Color. Red End Sub Chapter 3 23

Code for Walkthrough Private Sub txt. First_Leave(. . . ) Handles txt. First. Leave txt. First. Fore. Color = Color. Black End Sub Private Sub txt. First_Text. Changed(. . . ) Handles txt. First. Text. Changed txt. First. Fore. Color = Color. Blue End Sub Private Sub btn. Red_Click(. . . ) Handles btn. Red. Click txt. First. Fore. Color = Color. Red End Sub Chapter 3 24

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

Handling Multiple Events Event procedure can be invoked by two events. Private Sub Button_Click(. . . ) Handles btn. Red. Click, txt. Second. Leave txt. First. Fore. Color = Color. Red End Sub Chapter 3 26

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