List Boxes and Combo Boxes Loop Structures Printing PowerPoint PPT Presentation

presentation player overlay
1 / 51
About This Presentation
Transcript and Presenter's Notes

Title: List Boxes and Combo Boxes Loop Structures Printing


1
List Boxes and Combo Boxes Loop
StructuresPrinting
chapter
SEVEN
2
List Boxes Combo Boxes
  • Display list of items
  • List pre-defined or built dynamically
  • List of items may be read from files or databases

3
List Boxes vs. Combo Boxes
  • Both have
  • automatic scroll bars
  • .ListIndex property - an index to each item
  • .ListCount property - a number for the length of
    list
  • .List property - the list item itself
  • Combo has
  • Style property - type of combo box
  • .Text property - can be an editable field

4
List box and associated properties
l l l l l l l l
lstStates
List Property Selected Property
List(0) Washington Selected(0) False List(1)
Oregon Selected(1) False List(2)
California Selected(2) False List(3)
Alaska Selected(3) False List(4)
Hawaii Selected(4) False
Washington Oregon California Alaska Hawaii
ListCount Property 5
5
List box with two items selected
l l l l l l l l
lstStates
List Property Selected Property
List(0) Washington Selected(0) True List(1)
Oregon Selected(1) False List(2)
California Selected(2) False List(3)
Alaska Selected(3) False List(4)
Hawaii Selected(4) True
Washington Oregon California Alaska Hawaii
ListCount Property 5
6
List Box Multi-Select
  • 0 - None
  • One item only may be selected
  • 1 - Simple
  • Multiple items
  • Click toggles Select property
  • 2 - Extended
  • Multiple contiguous or non-contiguous selections

7
List Box Properties
  • ListCount
  • ListIndex
  • MultiSelect
  • Text
  • Sorted
  • List()
  • Selected()

8
List Box Methods
  • AddItem
  • lstBox.AddItem text
  • Always add a string to list box since it contains
    text
  • RemoveItem
  • lstBox.RemoveItem index
  • lstMembers.RemoveItem lstMembers.ListIndex
  • Clear
  • lstBox.Clear

9
Combo Box
  • List box with associated text box
  • Text box for entering new values (Styles 01)
  • List box for pre-defined values
  • Same properties as list box (Except no
    MultiSelect capability)
  • Same general methods
  • prefix convention cbo
  • To remove a selected item, use the ListIndex
    property
  • cboMembers.RemoveItem cboMembers.ListIndex

10
Examples of the dropdown Style properties
l l l l l l l l
The dropdown list style has almost the same
appearance as the dropdown combo style.
However, the user cannot type in the text box
portion of a dropdown list.
11
Combo Box Style property
  • 0 - DropDown Combo
  • Text box with down arrow
  • Click on down arrow to show list
  • 1 - Simple Combo
  • List box always displayed
  • 2 - DropDown List
  • User can not type into text box

12
Combo Box
Text property
  • cboFood.text ?
  • cboFood.listCount ?
  • cboFood.listIndex(3) ?
  • cboFood.listIndex ?
  • What happens with the code cboFood.Additem
    cboFood.Text
  • How many items are in the list?
  • What event is triggered as the user types in the
    word fig?

Fig
ListIndex
Apple Banana Carrot Donut Eggplant
0 1 2 3 4
ListCount 5
List
cboFood
13
Events
  • Change
  • triggers when the text changes
  • GotFocus
  • triggers when the list/combo/text box receives
    focus
  • LostFocus
  • triggers when list/combo/text box loses focus

14
PROGRAM CONSTRUCTS
  • Sequence
  • Decision (Selection)
  • Iteration
  • Pre-Test (While Loop)
  • Post-Test (Until Loop)

15
Sequence Structure
16
Decision Structure
17
(No Transcript)
18
Iteration
  • Purpose to have the computer repeat a certain
    set of steps over and over until some condition
    is true.
  • There are conditional loops and counted loops,
    but all are really conditional.

19
Iteration Basic Rules
  • The termination condition must be modified in the
    body of the loop
  • Otherwise, you have an infinite loop
  • Loops may be nested
  • Indent appropriately
  • Never change the value of the counter in a FOR
    loop

20
Determinate Loops For-Next Loops
  • FOR counter start TO end STEP increment
  • statements that compose the body of the loop
  • NEXT counter
  • Early exit from a loop
  • EXIT DO
  • EXIT FOR

21
Example of For-Next Loop
22
Action of For...Next structure
l l l l l l l l
23
Iterations of a For...Next loop
Private Sub cmdForNextTest_Click() Dim X As
Integer For X 1 To 3 MsgBox X Next X End
Sub
X
4
0 1
4 4
l l l l l
24
Iterations of a For...Next loop
Private Sub cmdForNextTest_Click() Dim X As
Integer For X 1 To 3 MsgBox X Next X End
Sub Private Sub cmdForNextTest_Click() Dim X As
Integer For X 1 To 3 MsgBox X Next X End
Sub
MsgBox Contents
X
4
1
0 1
4 4
MsgBox Contents
X
l l l l l
2
0 1 2
4
44
44
4
25
Iterations of a For...Next loop
Private Sub cmdForNextTest_Click() Dim X As
Integer For X 1 To 3 MsgBox X Next X End
Sub Private Sub cmdForNextTest_Click() Dim X As
Integer For X 1 To 3 MsgBox X Next X End
Sub Private Sub cmdForNextTest_Click() Dim X As
Integer For X 1 To 3 MsgBox X Next X End
Sub
MsgBox Contents
X
4
1
0 1
4 4
MsgBox Contents
X
l l l l l
2
0 1 2
4
44
44
4
MsgBox Contents
X
3
0 1 2 3
4
444
444
44
26
Iterations of a For...Next loop
l l l l l
27
Indeterminate Do-Loops
  • Indeterminate loops run for an unknown number of
    repetitions until a condition is true or while a
    condition is true
  • Four types of indeterminate loops
  • Until loop with termination condition before body
    of loop
  • While loop with termination condition before body
    of loop
  • Until loop with termination condition after body
    of loop
  • While loop with termination condition after body
    of loop
  • Pre-Test loops have termination condition before
    loop body
  • Post-test loops have termination condition after
    loop body

28
Pre -Test Loops
29
Iteration Pre-test Loops
  • Pretest
  • DO WHILE condition
  • statements
  • LOOP
  • DO UNTIL condition
  • statements
  • LOOP

30
(No Transcript)
31
Iteration Post-Test Loops
  • Post-test
  • DO
  • statements
  • LOOP WHILE condition
  • DO
  • statements
  • LOOP UNTIL condition

32
(No Transcript)
33
Iteration While Loops
  • WHILE loop (Pre-Test)
  • WHILE condition
  • statements
  • WEND
  • Will operate while condition is true (or until
    condition is false)

34
Form of Pre- and Post-Test Loops
  • The form of the pre-test loops is
  • Do Until (or While) condition
  • body of loop
  • Loop
  • The form of the post-test loops is
  • Do
  • body of loop
  • Loop Until (or While) condition

35
Action of DoLoop
l l l l l l l l
statement1 statement2 ...
36
Action of Do WhileLoop
l l l l l l l l
37
Iterations of the loop
Private Sub cmdDoWhileTest_Click() Dim X As
Integer Do While X lt 3 X X 1 MsgBox
X Loop End Sub
MsgBox Contents
X
4
1
0 1
4 4 4
l l l l l
38
Iterations of the loop
Private Sub cmdDoWhileTest_Click() Dim X As
Integer Do While X lt 3 X X 1 MsgBox
X Loop End Sub Private Sub cmdDoWhileTest_Click()
Dim X As Integer Do While X lt 3 X X
1 MsgBox X Loop End Sub
X
4
0 1
4 4 4
l l l l l
X
4
0 1 2
44
44
44
4
39
Iterations of the loop
Private Sub cmdDoWhileTest_Click() Dim X As
Integer Do While X lt 3 X X 1 MsgBox
X Loop End Sub Private Sub cmdDoWhileTest_Click()
Dim X As Integer Do While X lt 3 X X
1 MsgBox X Loop End Sub Private Sub
cmdDoWhileTest_Click() Dim X As Integer Do
While X lt 3 X X 1 MsgBox X Loop End Sub
X
4
0 1
4 4 4
l l l l l
X
4
0 1 2
44
44
44
4
X
4
0 1 2 3
444
444
444
44
40
Iterations of the loop
l l l l l
41
Action of Do...Loop While structure
l l l l l l l l
42
Action of Do Until . . . Loop structure
l l l l l l l l
Condition False
Condition True
Do Until condition statement1 statement2 . . .
Do Untilc ondition statement1 statement2 . . .
Loop
Loop statement
43
Equivalent Do While and Do Until loops
l l l l l l l l
44
Action of Do...Loop Until structure
l l l l l l l l
45
Debugging Loops
  • Debug a loop by inserting a debug.print command
    in the loop to print to the Immediate Window.
  • Add a Quick Watch by locating the pointer on a
    variable and clicking the eyeglass icon on the
    Debug Toolbar. The values for this variable will
    be shown in the Watch Window.
  • Use the Locals window to display the values of
    variables local to a procedure.
  • Use the Toggle Breakpoint icon to pause execution
    at a designated line in the code and then use the
    various windows to view the values for variables.

46
The Printer Object
  • Print Method -
  • Printer.Print
  • (sends output to printer object)
  • NewPage Method
  • Printer.NewPage
  • (send page to printer - starts another)
  • EndDoc Method
  • Printer.EndDoc
  • (send final page and terminate printer job)
  • For Debugging - To save paper
  • Debug.Print (displays in Immediate Window)

Printer.EndDoc Printer.NewPage
Printer.Print
47
Formatting
Printline Zone 1 Zone 2
Zone 3 12345678901234
12345678901234 12345678901234
  • Commas advance 1 zone (14 average characters)
  • Printer.Print Name , , Phone
  • Semicolons does not move to next zone
  • Printer.Print Name txtMyName
  • Ending Commas/Semicolons continue on print line
  • Printer.Print Name ,
  • Printer.Print txtMyName
  • Printer.Print by itself gives a blank line of
    output

48
Tab/Spc Functions
  • Tab and Spc (Space) functions allow programmer to
    override the defaults
  • Tab is absolute Spc is relative
  • Example
  • Printer.Print Tab(20) Name Spc(5)
    txtMyName
  • Places the word Name in the 20th column - an
    absolute placement - and the value of txtMyName
    in the 30th - 5 more relative to the end of
    Name

49
Printing a List
  • To print the contents of list box, use the
    ListCount and List() properties of the list box
  • ListCount is the number of items in list box but
    list() property runs from 0 to ListCount -1
  • List(Counter) is equal to the contents of the
    corresponding list box
  • Code to print contents of list box to Immediate
    Window
  • For Counter 0 to lstEntries.ListCount - 1
    Debug.Print lstEntries.List(Counter)
  • Next

50
Functions (Again??)
  • Intrinsic Functions are Very useful
  • Take a look at Appendix B for Date, Financial,
    Numeric and String Functions.
  • Payments -
  • PMT Function requires arguments -
  • interest rate, number of periods, amount of loan
  • Note that he PMT function will return a negative
    value
  • to display the values of variables local to a
    procedure.

51
Another Interesting Statement
  • Rset Statement (Which stands for Right Set)
  • Format
  • Rset string_variable string
  • where string_variable is the name of a fixed
    length string variable. These are dimensioned as
    follows
  • Dim strToFormat as String 10
  • 10 means that it will always be 10 characters
    long
  • where string is the expression you want right
    aligned. Example
  • Rset strToFormat Format(Payment, fixed)
  • discuss what this would do
Write a Comment
User Comments (0)
About PowerShow.com