chapter 31 fundamentals of programming in visual basic (vb) visual basic events simple statement

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

Upload: sylvia-daniel

Post on 18-Jan-2018

247 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement

Chapter 3 1

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

Page 2: Chapter 31 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

Page 3: Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement

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

Page 4: Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement

Chapter 3 4

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

Page 5: Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement

Chapter 3 5

Lab• Refers to the examples in the Lab

Page 6: Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement

Chapter 3 6

Sample Form

txtFirsttxtSecondbtnRed

Page 7: Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement

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.

Page 8: Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement

Chapter 3 8

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 txtBoxThis event occurs

when the input focus leaves txtBox

Page 9: Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement

Chapter 3 9

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.

Page 10: Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement

Chapter 3 10

Code Window

Method Name box

Class Name box

Page tab

Page tab: 1. frmDemo.vb is a code window2. frmDemo.vb [design] is a design window (to manipulate the window appearance)

Page 11: Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement

Chapter 3 11

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

Page 12: Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement

Chapter 3 12

Code Window

Method Name box

Class Name box

Page tab

Page 13: Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement

Chapter 3 13

Create an Outline for an Event Procedure; i.e. header and End Sub

1. Double-click on a control or2. Use the Class Name and Method Name boxes.

(We primarily use the first method.)

Page 14: Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement

Chapter 3 14

Sample Form

txtFirst

txtSecond

btnRed

Double Click on txtFirst

Page 15: Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement

Chapter 3 15

Code for Walkthrough

Public Class frmDemo Private Sub txtFirst_TextChanged(...) Handles txtFirst.TextChanged

End SubEnd Class

TextChanged event occurs when the user changes the text of a TextBox

Page 16: Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement

Chapter 3 16

Code for Walkthrough

Public Class frmDemo Private Sub txtFirst_TextChanged(...) Handles txtFirst.TextChanged txtFirst.ForeColor = Color.Blue End SubEnd Class

Page 17: Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement

Chapter 3 17

IntelliSense

Automatically pops up to give the programmer help.

Page 18: Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement

Chapter 3 18

Code Window

Click tab to return to Form Designer

Page 19: Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement

Chapter 3 19

Sample Form

txtFirst

txtSecond

btnRed

Double-click on btnRed

Page 20: Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement

Chapter 3 20

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 SubEnd Class

Page 21: Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement

Chapter 3 21

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 SubEnd Class

Page 22: Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement

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.

Page 23: Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement

Chapter 3 23

Code for WalkthroughPrivate Sub txtFirst_Leave(...) Handles txtFirst.Leave End Sub

Private Sub txtFirst_TextChanged(...) Handles txtFirst.TextChanged txtFirst.ForeColor = Color.BlueEnd Sub

Private Sub btnRed_Click(...) Handles btnRed.Click txtFirst.ForeColor = Color.RedEnd Sub

Page 24: Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement

Chapter 3 24

Code for WalkthroughPrivate Sub txtFirst_Leave(...) Handles txtFirst.Leave txtFirst.ForeColor = Color.BlackEnd Sub

Private Sub txtFirst_TextChanged(...) Handles txtFirst.TextChanged txtFirst.ForeColor = Color.BlueEnd Sub

Private Sub btnRed_Click(...) Handles btnRed.Click txtFirst.ForeColor = Color.RedEnd Sub

Page 25: Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement

Chapter 3 25

Header of Event ProcedurePrivate Sub btnRed_Click(…) Handles btnRed.Click

Identifies the event that

triggers the procedure

Name, can be changed.

Private Sub Button_Press(…) Handles btnRed.Click

Page 26: Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement

Chapter 3 26

Handling Multiple Events

Private Sub Button_Click(...) Handles btnRed.Click, txtSecond.Leave txtFirst.ForeColor = Color.RedEnd Sub

Event procedure can be invoked by two events.

Page 27: Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement

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“