Você está na página 1de 9

Event-driven Programming CSE 2149 Event Driven Programming

Lecture 1 Introduction

Event-driven programming is a programming paradigm in which the flow of the program is determined by events. Events are the actions that are performed by the user during the application usage.
E.g. If a user clicks a mouse button on any object then the Click event occurs. E.g. If a user moves the mouse then the mouse move event occurs. An application can generate Key down event, Key up event, mouse double click event.

If Block CSE 2149 Event Driven Programming


Lecture 1 VB Recap Control Statements

The program will take a course of action based on whether a condition is true. If condition Then action1 Else action2 End If
3

Will be executed if condition is true

Will be executed if condition is false

Another example If block


If condition Then action1 Regardless of whether End If Statement2 Statement3
the condition in the If statement is true or alse, these statements will be executed

Example 1: Form
txtFirstNum txtSecondNum txtResult

Assume the two numbers are not equal.


5 6

Example 1: Code
Private Sub btnFindLarger_Click(...) _ Handles btnFindLarger.Click Dim num1, num2, largerNum As Double num1 = CDbl(txtFirstNum.Text) num2 = CDbl(txtSecondNum.Text) If num1 > num2 Then largerNum = num1 Else largerNum = num2 End If txtResult.Text = "The larger number is " & largerNum End Sub
7

ElseIf clause
If condition1 Then action1 ElseIf condition2 Then action2 ElseIf condition3 Then action3 Else action4 End If
8

Example 2: Form
txtFirstNum txtSecondNum txtResult

Example 2: Code
Private Sub btnFindLarger_Click(...) _ Handles btnFindLarger.Click Dim num1, num2 As Double num1 = CDbl(txtFirstNum.Text) num2 = CDbl(txtSecondNum.Text) If (num1 > num2) Then txtResult.Text = "Larger number is " & num1 ElseIf (num2 > num1) Then txtResult.Text = "Larger number is " & num2 Else txtResult.Text = "The two are equal." End If End Sub
9 10

Select Case Syntax


The general form of the Select Case block is Select Case selector Case valueList1 action1 Case valueList2 action2 Case Else action of last resort End Select
11

Example 3: Form

txtPosition

txtOutcome

12

Example 3: Code
Private Sub btnEvaluate_Click(...) _ Handles btnEvaluate.Click Dim position As Integer = CInt(txtPosition.Text) Select Case position Selector Case 1 txtOutcome.Text = Gold" Case 2 txtOutcome.Text = Silver" Value Lists Case 3 txtOutcome.Text = Bronze" Case 4, 5 txtOutcome.Text = Better luck next time." Case Else txtBox.Text = You must try harder." End Select End Sub 13

Rules for Select Case


1. 2. 3. 4. 5. Case Else (and its action) is optional Each value list contains one or more of the following types of items separated by commas: a literal; a variable; an expression; an inequality sign preceded by Is and followed by a literal, variable, or expression; a range expressed in the form a To b, where a and b are literals, variables, or expressions.

14

Example 3b: Code


Private Sub btnEvaluate_Click(...) _ Handles btnEvaluate.Click Dim position As Integer = CInt(txtPosition.Text) Select Case position Case 1 To 3 txtOutcome.Text = Congratulations" Case Is >= 4 txtOutcome.Text = Better luck next time." End Select End Sub

Example 4: Partial Code


Dim x As Integer = 2, y As Integer = 3 Select Case num Case y - x, x txtPhrase.Text = "Buckle my shoe." Case Is <= 4 txtPhrase.Text = "Shut the door." Case x + y To x * y txtPhrase.Text = "Pick up sticks." Case 7, 8 txtPhrase.Text = "Lay them straight." Case Else txtPhrase.Text = "Start all over again." End Select
15 16

Question 1
Using IF statement, create a small application that allows you to input the mark of a student. Your application should grade the mark entered according to the following conditions and output the grade in a text box:
Marks (X) X >= 80 60>=X>80 40>=X>60 X<40 Grade A B C D

Question 2
Using case statements, create a small application that allows you to input the mark of a student. Your application should grade the mark entered according to the following conditions and output the grade in a text box:
Marks (X) X >= 80 60>=X>80 40>=X>60 X<40 Grade A B C D

Do Loop Syntax CSE 2149 Event Driven Programming


Lecture 1 VB Recap Loops

Do While condition statement(s) Loop


These statements are inside the body of the loop and are run if the condition above is true.

Condition is tested, If it is true, the loop is run. If it is false, the statements following the Loop statement are executed.

19

20

Example 5
Private Sub btnDisplay_Click(...) _ Handles btnDisplay.Click 'Display the numbers from 1 to 7 Dim num As Integer = 1 Do While num <= 7 lstNumbers.Items.Add(num) num += 1 'Add 1 to the value of num Loop End Sub
21

Example: Repeat Request as Long as Response in Incorrect


Dim passWord As String = "" Do While passWord <> VISUAL" passWord = InputBox("What is the password?") passWord = passWord.ToUpper Loop passWord is the loop control variable because the value stored in passWord is what is tested to determine if the loop should continue or stop.
22

Post Test Loop


Do statement(s) Loop Until condition

Post Test Loop


Dim passWord As String = "" Do While passWord <> VISUAL" passWord = InputBox("What is the password?") passWord = passWord.ToUpper Loop

Loop is executed once and then the condition is tested. If it is false, the loop is run again. If it is frue, the statements following the Loop statement are executed.

Rewrite the above program using a Post Test Loop

23

24

Example: Repeat Request Until Proper Response is Given


Dim passWord As String = " Do passWord = InputBox("What is the password?") passWord = passWord.ToUpper Loop Until passWord = VISUAL"

Example 6: Form

txtAmount

txtWhen

25

26

Example 6: Output

Example 6: Code
Private Sub btnCalculate_Click(...) Handles btnCalculate.Click Dim balance As Double, numYears As Integer balance = CDbl(txtAmount.Text) Do While balance < 1000000 balance += 0.06 * balance numYears += 1 Loop txtWhen.Text = "In " & numYears & _ " years you will have a million dollars." End Sub Chapter 5 - VB 2008 by Schneider 28

27

For

. Next Loop

Similar Do While Loop


i = 1 Do While i <= 5 lstTable.Items.Add(i & " " & i ^ 2) i += 1 Loop

For i As Integer = 1 To 5 lstTable.Items.Add(i & " " & i ^ 2) Next The loop control variable, i, is initialized to 1 tested against the stop value, 5 incremented by 1 at the Next statement

Write a Similar Do While Loop for the above For Next Loop
29 30

For Next Loop Syntax

Example
Control variable Data type Start value Stop value Amount to add to i

For i As Integer = 0 To n Step s lstValues.Items.Add(i) Next


31 32

Example: Nested Loops


For i As Integer = 55 To 70 For j As Integer = 1 To 25 Inner Outer lstBox.Items.Add(Chr(i) & j) loop loop Next Next OUTPUT: A1 A2 A3 :
33

Question 3
Using For loop, write a program that calculates the square of the numbers 1 to 10 and display them in a listbox as follows: The square of 1 is 1. The square of 2 is 4. The square of 3 is 9.
34

Question 4
Write a program that prompts the user to enter the number of marks he wants to compute and your program should then calculate the minimum mark, the maximum mark and the average mark.

Reference
Visual Basic 2008 by Schneider

35

36

Você também pode gostar