1 / 21

Lists, Loops, Printing

Lists, Loops,

beata
Download Presentation

Lists, Loops, Printing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


    1. Chapter 7 Lists, Loops, & Printing

    2. 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. 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. 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. 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 Add items to a list with code like this: lstSchools.AddItem “Stanford” lstSchools.AddItem txtSchoolName.Text lstSchools.AddItem cboFirstChoice.Text When a user types an entry in a combo box, that entry is not automatically added to the list. Code must be invoked to add the item (on the lost focus event, for example) cboIdeaList.AddItem cboIdeaList.TextAdd items to a list with code like this: lstSchools.AddItem “Stanford” lstSchools.AddItem txtSchoolName.Text lstSchools.AddItem cboFirstChoice.Text When a user types an entry in a combo box, that entry is not automatically added to the list. Code must be invoked to add the item (on the lost focus event, for example) cboIdeaList.AddItem cboIdeaList.Text

    6. 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 If the list is empty, then the ListCount is zero You can deselect all items in a list by setting the ListIndex property to -1 Set the ListIndex property to 0 to select (highlight) the first item in the list--remember that indexes run from 0 to n-1 ListCount holds the number of items in a list. Remember that ListCount is always one higher than the highest ListIndex. Display one of the items in a list by selecting the list object and referring to the ListIndex value as an index: lstBookTypes(iValue) Add items to the list in code with: lstSchools.AddItem “Purdue” lstSchools.AddItem “University of San Diego” The Sorted property of the list keeps items in alphabetic order. Copy selected list item to a text box: txtSelection.text = cboFruit.List(3) txtSelection.text = cboFruit.List(cboFruit.ListIndex) Setting a list index to a value selects—highlights—the list item: lstCoffeeTypes.ListIndex = 3 ‘Highlight the 4th item in list lstCoffeeTypes.ListIndex = -1 'Deselect all in list If the list is empty, then the ListCount is zero You can deselect all items in a list by setting the ListIndex property to -1 Set the ListIndex property to 0 to select (highlight) the first item in the list--remember that indexes run from 0 to n-1 ListCount holds the number of items in a list. Remember that ListCount is always one higher than the highest ListIndex. Display one of the items in a list by selecting the list object and referring to the ListIndex value as an index: lstBookTypes(iValue) Add items to the list in code with: lstSchools.AddItem “Purdue” lstSchools.AddItem “University of San Diego” The Sorted property of the list keeps items in alphabetic order. Copy selected list item to a text box: txtSelection.text = cboFruit.List(3) txtSelection.text = cboFruit.List(cboFruit.ListIndex) Setting a list index to a value selects—highlights—the list item: lstCoffeeTypes.ListIndex = 3 ‘Highlight the 4th item in list lstCoffeeTypes.ListIndex = -1 'Deselect all in list

    7. 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 Change event occurs whenever the user types anything into a combo box (styles 0 and 1). Each keystroke generates another change event. A list box does not have a change event, because list boxes do not have text boxes—only fixed lists that the user cannot add to by typing at runtime. GotFocus event occurs when a control—a list box or combo box for example—receives control. LostFocus event occurs as the control loses the focus—when the user tabs to another control on the form for example.Change event occurs whenever the user types anything into a combo box (styles 0 and 1). Each keystroke generates another change event. A list box does not have a change event, because list boxes do not have text boxes—only fixed lists that the user cannot add to by typing at runtime. GotFocus event occurs when a control—a list box or combo box for example—receives control. LostFocus event occurs as the control loses the focus—when the user tabs to another control on the form for example.

    8. 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) Loop structure for pretest form: Do While <condition> … LoopLoop structure for pretest form: Do While <condition> … Loop

    9. 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! Loop structure for posttest form: Do … Loop While <condition> Loop structure for posttest form: Do … Loop While <condition>

    10. 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. 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 Looping through a list: Dim intLoopIndex As Integer For intLoopIndex = 0 To lstSchools.ListCount - 1 (body of loop here) Next intLoopIndex Loop test is for greater than limit, not greater than or equal to limitLooping through a list: Dim intLoopIndex As Integer For intLoopIndex = 0 To lstSchools.ListCount - 1 (body of loop here) Next intLoopIndex Loop test is for greater than limit, not greater than or equal to limit

    12. 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 Example of an infinite (endless) loop: Dim intLoop As Integer For intLoop = 0 To 10 Step 1 intLoop = 1 Next The Exit For provides a convenient way to “hop out” of a loop when the code recognizes a special condition: For intIndex = 0 To 1000 Step 1 If lstItems.List(intIndex) = txtPersonsName.Text Then bFoundNameInList = True Exit For ‘Leave the for loop early--found name! End If Next intIndex ‘Keep searching Other forms of early exis from structures include: Exit Do Exit Sub Exit FunctionExample of an infinite (endless) loop: Dim intLoop As Integer For intLoop = 0 To 10 Step 1 intLoop = 1 Next The Exit For provides a convenient way to “hop out” of a loop when the code recognizes a special condition: For intIndex = 0 To 1000 Step 1 If lstItems.List(intIndex) = txtPersonsName.Text Then bFoundNameInList = True Exit For ‘Leave the for loop early--found name! End If Next intIndex ‘Keep searching Other forms of early exis from structures include: Exit Do Exit Sub Exit Function

    13. 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. MsgBox function can be invoked like this also: If MsgBox(strMessage, vbOkCancel) = vbOK Then...MsgBox function can be invoked like this also: If MsgBox(strMessage, vbOkCancel) = vbOK Then...

    14. MsgBox Values Constant Value Button Pressed vbOK 1 OK vbCancel 2 Cancel vbAbort 3 Abort vbRetry 4 Retry vbIgnore 5 Ignore vbYes 6 Yes vbNo 7 No (ignore the Value column; use intrinsic constants)

More Related