Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 31

Chapter 6: List, Loops and Printing

With reference to: Bradley | Millspaugh


Chapter Outline
 List Boxes and Combo Boxes
 Loops
 MsgBox Function
 String Function
 Print Method
List Boxes and Combo Boxes
 List Box: With the ListBox control the user can select
items from a list of choices.
 The common technique for loading a ListBox is to do it in
code, with the Form_Load event. This way, if items have
to be added it is a simple matter to add a line of code.
 In the example shown, AddItem is a Method because it is the action of
adding items to the ListBox.
If you wanted to remove an item from the list in code, there is a
RemoveItem Method for the ListBox. Eg: lst_team.RemoveItem 2
would remove the 3rd team - remember that it starts at 0.
lst_team.RemoveItem lst_team.ListIndex ‘removes the currently
selected team.
 When the Form opens, it will load the list in Form_load before the
ListBox is displayed. If there are too many items for the space allocated
to the ListBox, it will create a vertical scroll bar.
 When the user selects one of the teams from the list, we have to have a
way of capturing that information in a variable. That is done with the
Text property of the ListBox:
TeamName = lst_team.Text
 Combo box: a combination of a ListBox and a TextBox.
 It has the advantage over the ListBox of not taking up space until it is
actually used which means that it makes it easier to position on the Form.
 As in the ListBox, use the Text property to get the information input.
Label3.Caption = cbo_position.Text

 Clear Method: to empty a combo box or list box


 Eg:listCoffeeTypes.Clear
 When a project is running and the user selects and item from the list, the index
number of that item is stored in th ListIndex property.
 Eg: lstCoffeeTypes.ListIndex = 3 ‘Select the 4th item in the list
 lstCoffeeTypes.ListIndex = -1 ‘Deselect all items in the list
 List property: To display one item from a list
 Eg: lstSchools.List(5) = “UiTM”
 lblUniv.Caption = cboUniv.List(intIndex)
Loops
 Loops provide the ability to repeatedly execute the same
block of code, and to each time change values such that
each run through the loop produces different results.
Visual Basic provides four main kinds of loops:
 Do-Loop,
 Do-Until Loop,
 Do-While Loop,
 For-Next Loop.
Do/Loops
 This, quite simply, executes the block of code, and when it
reaches Loop, returns to the beginning of the Do Loop and
executes the same block of code again. The same block of
code will be repeatedly executed until it is told to stop
executing.

Do
    (Code to execute)
Loop
Dim number As Long
number = 0
Do
number = number + 1
Print number
Loop
Results: continually generate and print out the next number
infinitely
 Exit Do
So we clearly need some way to escape from the Do-
Loop. You could, of course, simply End the program once
you have calculated enough values, but what if you still
need to perform tasks after you're done calculating? The
answer is to use the Exit Do statement. Whenever your
program reaches an Exit Do statement within a loop, it
will exit the current loop.
Dim number As Long
number = 0
cnt = 1 Results:
Do 1
number = number + 1 2
3
Print number 4
5
If cnt >= 8 Then 6
7
Exit Do
8
Else
cnt = cnt + 1
End If

Loop
Do-Until/Loop

 Do Until
    (Code to execute)
Loop

•(Expression) can be any legal logical expression that we wish to


evaluate to determine whether or not to exit the loop.
•Each time the program reaches Loop it will evaluate this
expression.
•If the expression is True, it will exit the loop for us, but otherwise
it will continue looping
Dim number As Long
Results:
number=0 1

Do Until number > 10 2


3
number = number + 1 4
Print number 5
6
Loop 7
8
9
10
11
 Boolean Data Type
Dim blnItemFound as Boolean
blnItemFound = False
Do Until blnItemFound ‘Checks for True
……
Do-While/Loop

 Do While
    (Code to execute)
Loop

•(Expression) can be any legal logical expression that we


wish to evaluate to determine whether or not to exit the
loop.
•Each time the program reaches Loop it will verify that
this expression is True, and if it is False, it will exit the
loop for us.
•Thus, instead of exiting when an expression is True, it
now exits only once this expression is false.
Dim number As Integer
Results:
number = 1 2
Do While number <= 10 3
4
number = number + 1 5
6
Print number 7
Loop 8
9
10
11
For/Next Loops
 Use in situations where you merely want to run the loop a
predefined number of times.
 Uses a counter variable, called the loop index –
determines the number of times the statements inside the
loop to be executed.
 This kind of loop allows you to specify a counter, to tell it
to count from one number to another each time through
the loop, and to exit once the counter has reached its upper
limit.
 For I = (Integer) To (Integer)
   (Code to execute)
Next I 
 Exit For
- Use on occasion when you need to terminate a loop before
the loop index reaches its final value.
 A counter controlled loop generally has three elements
1) Initialize the counter
2) Increment the counter
3) Test the counter to determine when it is time to terminate the
loop.
Dim x As Integer Results:
1
For x = 1 To 5 2
Print x 3
4
Next 5

For x = 1 To 5 Results:
Print x ; 12345
Next
Results:
1
For x = 1 To 5 Step 2 3
Print x 5
Next
Results:
 For x = 5 To 1 Step -2 5
Print x 3
1
Next
 Exit For
For intLoopIndex = 1 To 10
If Text1.Text = “” Then
MsgBox “You must enter something”
Exit For
End If
Next intLoopIndex
MsgBox Function
 Display a dialog box with more than one button
 The functions returns a value to indicate which button was
pressed.
Dim intResponse As Integer
intResponse = MsgBox("Clear the coffee list?", vbYesNo +
vbQuestion, "Title")
If intResponse = vbYes Then
Combo1.Clear
End If
Important button and icon values:
Button to Display Intrinsic Constant
OK vbOKOnly
OK and Cancel vbOKCancel
Abort, Retry, Ignore vbAbortRetryIgnore
Yes, No, Cancel vbYesNoCancel
Yes and No vbYesNo
Retry and Cancel vbRetryCancel
Important message icons and their intrinsic constants
Icons to DisplayIntrinsic Constant
Critical Message vbCritical
Warning Query vbQuestion
Warning Message vbExclamation
Information Message vbInformation

 Examples of using instinsic constants:


 intResp = MsgBox(strMess, vbYesNoCancel + vbQuestion, ”Do What?”)
 intResp = MsgBox(strMess, vbCancel + vbCritical, ”Big Problems”)
 intResp = MsgBox(strMess, vbOKOnly + vbInformation, ”FYI”)
String Function
 A part of the string; return a specifies section of a string
 Left(StringExpression, NumberOfCharacters)
 Mid(StringExpression, StartPosition, [NumberOfCharacters])
 Right(StringExpression, NumberOfCharacters)
 Len(StringExpression)

 StringExpression – string variable, string literal or text property


 NumberOfCharacters and StartPosition – both numeric; variable,
literal or numeric expressions
 Mid – if you omit the NumberOfCharacters argument, the
function returns all characters starting with the StartPosition.
 Len – To determine the length of a string expression.
 Len(str) returns # chars in string
Len(“Visual Basic”) returns 12
 SelStart property : return the position of the first selected character
 SelLength property: return the number of selected character

Private Sub textName_GotFocus()


‘Select the current entry
With txtName
.SelStart = 0 ‘begin selection at start
.SelLength = Len(.Text) ‘select the number of characters
End With
End Sub

 http://www.vb6.us/tutorials/vb6-string-functions
 Left(txtName.Text, 3) is Hel
(returns first 3 characters)
 Right(txtName.Text, 3) is rld

(return last 3 characters)


 Mid(txtName.Text, 6, 5) is world

(returns 5 characters beginning with character 6


 Mid (txtName.Text, 4) is o World

( returns all characters beginning with character 4)

Hello World
Print Method
 To print text in a form, on the printer object or the debug
window

 http://www.informit.com/library/content.aspx?b=STY_V
B6_24hours&seqNum=169
 Printer.print – output added to the printer object
 Methhod: EndDoc, NewPage – job terminates, send
content in printer object to printer.
 Formatting lines

The output page has preset tab settings with 5 columns per
Line (print zone) Use a comma to advance the output to
the next print zone. Eg: Printer.Print 1, 2, 3, 4, 5
Output:
1 2 3 4 5
 Use a semicolons to separate items without advancing to
the next print zone. Eg: Printer.Print “Name: “; Text1.Text
 Output: Name: Hatie
 Trailing commas and semicolons – continue on the same
line without advancing the line
 Eg: Printer.Print “ Harimau”,

Printer.print “Malaya”
 Output: Harimau Malaya
 Printer.Print – print a blank lines
 Tab function – to specify the column position where you
want the output to appear. Eg: Printer.Print Tab (20);
“Hello Guys”
 Output: Hello Guys
 Spc function – to specify the number of spaces on the line
that you want to advance from the last item printed. Eg:
Printer.Print Tab(20); “Name”; Spc(5); “Phone”; Spc(5);
“Address”
 Output: Name Phone Address
 Aligning String and Numeric data
 String values: print with no extra spacing between lines

Eg: Printer.Print “Favorite”; “Person”


Output: FavoritePerson
 Numeric data: print with additional spacing

Eg: Printer.Print 1; 2; 3; -1; -2; -3


Output: 1 2 3 -1 -2 -3
 Can combine printing string and numeric data

Eg: Printer.Print “Item”, “Quantity” Output:


Item Quantity
Printer.Print “Scissors”, 10 Scissors 10
Printer.Print “Rocks”, -2 Rocks -2
Porcupines 1
Printer.Print “Porcupines”, 1
 Picture Box – Picture1.Printer “haiiiiiiiiiiiii”
 Change font – change only once before the first item you
print. Eg: Printer.Font.Name = “Arial”
Printer.Font.Size = 12
 NewPage - sends the current page to the printer and
clears the printer object in memory to begin a new page
 EndDoc – sends the current page to the printer and
terminates the printer job
Printer.NewPage
Printer.EndDoc

You might also like