VB Lecture 1 - American University of Beirut

Download Report

Transcript VB Lecture 1 - American University of Beirut

CVEV 118/698
Visual Basic
Lecture 2
Prof. Mounir Mabsout
Elsa Sulukdjian
Walid El Asmar
Control Flow Statements


An important feature in programming languages
is the ability to examine external conditions and
act accordingly.
VB provides three control flow (or decision)
statements:
– If … Then … End If
– If … Then … Else … End If
– Select Case
(Elseif)
If/Then Statements

Example 1:
If intAnswer = 1 Then
strMood = “happy”
End If

Example 2:
If intAnswer = 1 Then
strMood = “happy”
Else
strMood = “sad”
End If

Example 3:
If intAnswer = 1 Then
strMood = “happy”
Elseif intAnswer = 2 Then
strMood = “sad”
Else
strMood = “don’t care”
End If
Select Case Statement

Efficient method for multi-conditional cases.
Select Case expression
Case Value_1
Statement block_1
Case Value_2
Statement block_2
…
Case Value_n
Statement block_n
Case Else
Statement block
End Select
Select Case WeekDay(intDate)
Case 1
strDayName = “Monday”
strMessage = “Have a nice week”
Case 6
strDayName = “Saturday”
strMessage = “Have a nice weekend”
Case 7
strDayName = “Sunday”
strMessage = “Working on Sunday!!??”
Case Else
strMessage = “Welcome back”
End Select
Select Case (cont’d)



The Select Case structure compares the results of one
expression to several values, and if it matches one of
them, the corresponding block of statement is
executed.
If more than one Case value matches the expression,
only the statement block associated with the first
matching Case executes.
Disadvantage: The Select Case evaluates the expression
before entering the Case statements, while the
If/Then/Else can evaluate a different expression for
each ElseIf statement.
Select Case (cont’d)

Example 2
Select Case WeekDay(intDate)
Case 1, 2, 3, 4, 5
strDayType = “Workday”
strMessage = “Enjoy Work”
Case 6, 7
strDayType = “Holiday”
strMessage = “Enjoy your weekend”
Case Else
strDayType = “VBday”
strMessage = “Goto your assignment!”
End Select
Loop Statements

Loop Statements allow to execute code
repetitively.

VB provides three loop statements:
– Do … Loop
– For … Next
– While … Wend
Do … Loop

Syntax 1:
Do While condition
Loop
statement block

VB evaluates the condition, if it is False the block is
never executed. If it is True, VB executes the statement
block, re-evaluates the condition, and repeats the
statement block if it is True.

Example 1:
Do While dblGrade <= 80
Loop
dblGrade = dblGrade + 1
MsgBox “Work Harder!”
Do … Loop (cont’d)

Syntax 2:
Do
statement block
Loop While condition

The main difference with syntax 1 is that the statement
block is executed the first time independently of the
condition. The evaluation starts before the second loop.

Example 2:
Do
dblGrade = dblGrade + 1
MsgBox “Work Harder!”
Loop While dblGrade <= 80
For … Next



The loop executes depending on a preset
number of times not on a conditional statement.
It uses a variable (counter) that increases or
decreases in value each time the statement is
executed.
Syntax:
For counter = start To end [Step increment]
statements
Next [counter]
For … Next (cont’d)

Example 1:
For intNum = 1 to 10
intSquareNum(intNum) = intNum * intNum
Next intNum


Unless specified, the incremental step of the
counter (I.e. intNum) is 1 by default.
Example 2:
For intNum = 10 to 1 Step -2
intSquareNum(intNum) = intNum * intNum
Next intNum
While … Wend


While…Wend executes a block of statements
while a condition is true.
Syntax:
While condition
statement block
Wend

Why all this trouble?
Just use the Do While Loop 
Functions & Subroutines


VB code is not a monolithic listing. An
application in VB is made up of small self
contained segments that are “event handler”.
Difference between a function & a subroutine:
– A Function executes a series of commands and
returns a value.
– A Subroutine can be considered a special function
since it does not return anything.
Subroutines

A subroutine is a block of statements that carry
out a well-defined task.
Sub MyFirstSub ()
……
……
End Sub

All the event procedures in VB are coded as
subroutines e.g.: the “click” event of a button.
Functions

A function returns a result, accordingly it must
have a type:
Function MyFirstFunction () As Double
……
……
End Function
Arguments


Arguments are values passed to a procedure (a
function or subroutine) and on which the procedure
acts.
Example:
Function dblCircleArea (r As Double) As Double
dblCircleArea = pi * r * r
End Function
To call the above function:
dblCArea1 = dblCircleArea (dblRadius)
(where dblCArea1 & dblRadius are previously defined variables of
type double)
Arguments (cont’d)


The number of arguments passed must be the same
as the number of arguments defined in the function
or subroutine.
Example:
Function dblRectArea (a, b As Double) As Double
dblRectArea = a * b
End Function
‘Error will be generated in the following code:
dblAreaWrong = dblRectArea(dblLength)
‘The right piece of code is:
dblAreaRight = dblRectArea(dblLength,dblWidth)
Optional Arguments
You may need a function to handle in some cases
one argument; in other cases two or more.
 Then you need to use the “Optional argument”:

Function dblRectArea (a As Double, Optional b As Double) As
Double
dblRectArea = a * a
If IsMissing(b) Then Exit Function
dblRectAtea = a * b
End Function
Both calls for the above function are valid:
RArea1 = dblRectArea(dblLength)
RArea2 = dblRectArea(dblLength,dblWidth)
Forms
A form is the “container” which includes all the
“controls” that make up the user interface.
 Forms have a built-in functionality that is always
available without any coding from you (title bar,
resize, etc.).
• Properties of a form exist for you to customize its
appearance. Some of these properties are:

– MinButton, MaxButton
– BorderStyle
– ControlMenu
Forms (cont’d)
Title
Bar
Control
Menu
Minimize
Maximize
Close
Basic Controls



Controls are another key factors in the “eventdriven” programming.
VB has a multitude of controls, and many third
party companies are developing more.
You can create your own control.
Frames and Option Boxes


Rectangular entity inside a form that groups one
or more controls.
Frames are used for esthetics reasons and/or for
proper functioning of option boxes: inside each
frame, only one option box can be ticked
Frames and Option Boxes:
Example
TextBox Properties







Multiline: set to true if you want to display correctly
multiple lines.
PasswordChar: set to a character that gets displayed to
hide the real characters.
ToolTipText: Yellowish text box that explains about a
control.
ScrollBars: Controls the attachment of scroll bars, if the
text exceeds the control dimensions.
MaxLength: Maximum number of characters contained.
TabStop: Controls the access to the control with the tab
key.
Enabled: Controls the access to the control
What’s Next



Form manipulation
Menu design
Other controls