Escolar Documentos
Profissional Documentos
Cultura Documentos
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.
The program will take a course of action based on whether a condition is true. If condition Then action1 Else action2 End If
3
Example 1: Form
txtFirstNum txtSecondNum txtResult
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
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
14
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
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
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.
23
24
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
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
Example
Control variable Data type Start value Stop value Amount to add to i
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