Download presentation
Presentation is loading. Please wait.
Published byPolly Carr Modified over 9 years ago
1
Irwin/McGraw-Hill Copyright© 2000 by the McGraw-Hill Companies, Inc. PowerPoint® Presentation to accompany prepared by James T. Perry University of San Diego
2
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Ch 7: Lists, Loops, & Printing List Boxes & Combo Boxes Populating list boxes and combo boxes Do Loops, For/Next Loops MsgBox returned values String Functions Send Info. to the Printer with Print method Formatting Output
3
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill List Boxes & Combo Boxes List/Combo boxes allow a list of items Types include –simple combo boxes –dropdown combo boxes –dropdown lists Combo box has style property; list does not Combo box has design-time text property; list box has only run-time text property. Combo box does not have a MaxLength property; a list box does.
4
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Filling the List You can fill the list at design- or run-time Fill at design time if list rarely changes –Click List property, type entry, press Ctrl+Enter –Make Sorted property true to keep list in order Fill at run time if list is volatile –Use AddItem method to add items –Form: object.AddItem value [,Index] –Example: lstFruits.AddItem "Apple"
5
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill List/Combo Box Methods AddItem - Add item to list cboFruits.AddItem "Kiwi" cboFruits.AddItem cboFruits.Text Clear - Delete all items from the list cboFruits.Clear
6
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill List/Combo Box Properties Sorted maintains list in order (if true) ListIndex holds user-selected item index number (begins at zero, -1 if none selected) ListCount is number of items in list List object.List(index) extracts string value of list item Setting the ListIndex property highlights a selection in the list
7
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill List/Combo box Events Change Occurs when user types text into combo box (list box does not have this) GotFocus Occurs when control receives control LostFocus Occurs when control loses control
8
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Do Loops (1) Allow repeated execution of instructions Do loop terminates on a condition you specify Do loop can test for condition at –Top of loop (pretest) –Bottom of loop (posttest)
9
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Do Loops (2) Loops can continue while a condition is either true or false –Go while True:…While condition –Go while False:…Until condition Be sure to change the loop variable within the loop or it will loop infinitely!
10
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill For/Next Loops (1) For/Next allows you to repeat series of instructions using a counter (a loop index) Form: For loopindex = initialvalue To endvalue [Step k] …body of loop… Next [loopindex]
11
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill For/Next Loops (2) You can loop “backwards” by using a negative increment Loop testing is done before executing body Do not change the loop control inside the body Halt infinite loops by pressing Ctrl+Break Exit For allows early For loop exit from within the body
12
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill For/Next Loops (3) Use For/Next loop when you know the number of times a loop will run When loop decrements, the test is for strictly less than the ending value The For/Next ending condition may exist before entering the loop. If so, then the loop body is skipped
13
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Using the MsgBox Function MsgBox has a function form (we have used statement form until now) MsgBox function returns which key the user pressed of those available Use VB intrinsic constants to determine which key was pressed.
14
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill MsgBox Values ConstantValueButton Pressed vbOK 1OK vbCancel 2Cancel vbAbort 3Abort vbRetry 4Retry vbIgnore 5Ignore vbYes 6Yes vbNo 7No (ignore the Value column; use intrinsic constants)
15
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Using String Functions Left(str, nchars) - extract left nchars Right(str, nchars) - extract right nchars Mid(str,start,[nchars]) - get middle chars str is any string, nchars is integer, and start is integer number of starting position. Len(str) returns # chars in string
16
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Using String Functions (2) String functions and looping facilitate the “zooming” feature of many Microsoft list boxes and combo boxes. “Zooming” causes a search of the list, refining the search with each additional keystroke the user enters.
17
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Sending Info. to the Printer Visual Basic’s Print method applies to a Form, a printer object, the Debug window, or a picture box Printer.Print — sends output to printer Print is a method of the Printer object Printing is somewhat difficult, because VB is designed to use with forms, not printers
18
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Formatting Lines Printer.print x, y, z prints in three "zones" Printer.print x;y;z ignores zones Trailing comma or semicolon causes next print method to continue the line Each.print starts on a new line Printer.print (alone) prints blank line
19
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Formatting Lines Tab(20) advances to 20th position: Printer.Print Tab(20); "Name:" Spc(17) inserts spaces (17) in line Numbers print with one space for sign of # Printer.NewPage sends page to printer & goes to new page Printer.EndDoc sends page to printer & halts print job
20
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Printing to Picture Box You can use Print method with a picture box to avoid actually printing a result: picMyPictureBox.Print "howdy" prints to a picture box
21
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Hands-On Programming Example
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.