0% found this document useful (0 votes)
9 views22 pages

Second Lecture Note VB

The document explains various programming control structures in Visual Basic, including Select Case statements, For/Next loops, and Do/While loops. It also covers key trapping for user input, the use of message boxes, and the implementation of frames and check boxes for user interface design. Additionally, it highlights the importance of managing loops to avoid infinite loops and provides examples for each concept discussed.

Uploaded by

Safiyya Mustapha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views22 pages

Second Lecture Note VB

The document explains various programming control structures in Visual Basic, including Select Case statements, For/Next loops, and Do/While loops. It also covers key trapping for user input, the use of message boxes, and the implementation of frames and check boxes for user interface design. Additionally, it highlights the importance of managing loops to avoid infinite loops and provides examples for each concept discussed.

Uploaded by

Safiyya Mustapha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Select Case - Another Way to Branch

• In addition to If/Then/Else type statements,


the Select Case format can be used when
there are multiple selection possibilities.
For/Next loop with Step.

Example
For I = 1 to 50 Step 2
A=I*2
Debug.Print A
Next I
• You may exit a For/Next loop using an Exit For
statement. This will transfer program control to
the statement following the Next statement.
Count.
If Age = 5 Then
Category = "Five Year Old"
ElseIf Age >= 13 and Age <= 19 Then
Category = "Teenager"
ElseIf (Age >= 20 and Age <= 35) Or Age = 50 Or (Age >= 60 and Age <=
65) Then
Category = "Special Adult"
ElseIf Age > 65 Then
Category = "Senior Citizen"
Else
Category = "Everyone Else“
End If
Example of case statement
Select Case Age
Case 5
Category = "Five Year Old"
Case 13 To 19
Category = "Teenager"
Case 20 To 35, 50, 60 To 65
Category = "Special Adult"
Case Is > 65
Category = "Senior Citizen"
Case Else
Category = "Everyone Else"
End Select
Do While/Loop
Do While/Loop Example:
Counter = 1
Do While Counter <= 1000
Debug.Print Counter
Counter = Counter + 1
Loop
Do Until/loop
Counter = 1
Do Until Counter > 1000
Debug.Print Counter
Counter = Counter + 1
Loop
• This loop repeats Until the Counter variable
exceeds 1000. Note a DoUntil/Loop structure
will not be entered if the Until condition is
already True on the first encounter.
Do/Loop While
Do/Loop While Example:
Sum = 1
Do
Debug.Print Sum
Sum = Sum + 3
Loop While Sum <= 50

• Note since the While check is at the end of the loop, a


Do/Loop While structure is always executed at least
once.
Do/Loop Until
Do/Loop Until Example:
Sum = 1
Do
Debug.Print Sum
Sum = Sum + 3
Loop Until Sum > 50
• This loop repeats Until Sum is greater than 50.
And, like the previous example, a Do/Loop Until
structure always executes at least once.
Infinite loop
• Make sure you can always get out of a loop!
Infinite loops are never nice. If you get into
one, try Ctrl+Break. That sometimes works -
other times the only way out is rebooting your
machine!
• The statement Exit Do will get you out of a
loop and transfer program control to the
statement following the Loop statement.
KEY TRAPPING
• Whenever getting input from a user, we want to limit the available
keys they can press. The process of interecepting unacceptable
keystrokes is key trapping.

• Key trapping is done in the KeyPress procedure of an object. Such a


procedure has the form (for a text box named txtText):
Sub txtText_KeyPress (KeyAscii as Integer)
.
.
.
End Sub
KEY TRAPPING EXAMPLE
Private Sub txtNumber1_KeyPress(KeyAscii As
Integer)
If KeyAscii = 13 Then
txtNumber2.Visible = True
lblNumber2.Visible = True
txtNumber2.SetFocus
End If
End Sub
Count.
In key trapping, it's advisable to always allow
the backspace key (ASCII code 8; symbolic
constant vbKeyBack) to pass through the key
press event. Else, you will not be able to edit
the text box properly.
MESSAGE BOX
• One of the best functions in Visual Basic is the message box.
The message
• box displays a message, optional icon, and selected set of
command buttons.
• The user responds by clicking a button.
• The statement form of the message box returns no value (it
simply displays the box):
• MsgBox ( Message, Type, Title)
Message: Text message to be displayed
Type: Type of message box (discussed in a bit)
Title: Text in title bar of message box
Count.
• The Type argument is formed by summing four values corresponding
to the buttons to display, any icon to show, which button is the
default response, and the modality of the message box.
• The first component of the Type value specifies the buttons to display:
• Value Meaning Symbolic Constant
0 OK button only vbOKOnly
1 OK/Cancel buttons vbOKCancel
2 Abort/Retry/Ignore buttons vbAbortRetryIgnore
3 Yes/No/Cancel buttons vbYesNoCancel
4 Yes/No buttons vbYesNo
5 Retry/Cancel buttons vbRetryCancel
count
• The second component of Type specifies the
icon to display in the message box:
Value Meaning Symbolic Constant
0 No icon (None)
16 Critical icon vbCritical
32 Question mark vbQuestion
48 Exclamation point vbExclamation
64 Information icon vbInformation
Count.
• The third component of Type specifies which
button is default (i.e. pressing Enter is the
same as clicking the default button):
Value Meaning Symbolic Constant
0 First button default vbDefaultButton1
256 Second button default vbDefaultButton2
512 Third button default vbDefaultButton3
Count.
• The fourth and final component of Type
specifies the modality:
Value Meaning Symbolic Constant
0 Application modal
vbApplicationModal
4096 System modal vbSystemModal
Message Box Example:

• MsgBox “This is an example of a message


box”, vbOKCancel + vbInformation, “Message
Box Example”
THE USE OF FRAMES

• The main use of a frame is to enclose other


objects, such as option buttons or check boxes,
or to hold labels and text boxes where the label
serves as a prompt or input for the text box.

• Once a frame has been set up objects cannot
be moved out of it at run time, nor can you
draw other objects and put a frame round
them afterwards, the frame must come first.
Frames and option buttons

• Frames are especially useful for enclosing


option buttons, the reason is that in a set of
option buttons only one can be selected at any
one time. By enclosing two sets of option
buttons in frames, one can be selected from
one set, and another from the other set.
THE USE OF CHECK BOXES

• The principal difference between option


buttons and check boxes is that check boxes
allow the user to select several different
alternatives during the same program.
USING SCROLL BARS
Moving an object

• The ‘Move’ function can be used by either


horizontal or vertical scroll bars to manipulate
the position of an object on screen.

You might also like