Upload
bethanie-elliott
View
213
Download
0
Embed Size (px)
DESCRIPTION
7-3 Objectives (1 of 2) Create and use list boxes and combo boxes Differentiate among the available types of combo boxes Enter items into list boxes using the Items collection in the Properties window Add and remove items in a list at run time Determine which item in a list is selected Use the Items.Count property to determine the number of items in a list
Citation preview
7-1
aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf
Lists, Loops, and Printing
Chapter7
McGraw-Hill © 2006 The McGraw-Hill Companies, Inc. All rights reserved.
7-3
Objectives (1 of 2)
Create and use list boxes and combo boxesDifferentiate among the available types of combo
boxesEnter items into list boxes using the Items collection in
the Properties windowAdd and remove items in a list at run timeDetermine which item in a list is selectedUse the Items.Count property to determine the
number of items in a list
7-4
Objectives (2 of 2)
Display a selected item from a listUse Do/Loops and For/Next statements to iterate
through a loopTerminate a loop with the Exit statementSkip to the next iteration of a loop by using the
Continue statementSend information to the printer or the Print Preview
window using the PrintDocument class
7-5
ListBoxes and ComboBoxes (1 of 2)
Have most of the same properties and operating in a similar fashionAn exception is that a combo box control has a
DropDown Style propertyProvide the user with a list of items to select fromVarious styles, choose based on
Space availableNeed to select from an existing listNeed to add to a list
7-6
List Boxes and ComboBoxes (2 of2)
Various Styles of List and Combo boxes
7-7
The Items Collection
List of items in a ListBox or ComboBox is a collection
VB Collections are objects that have properties and methods that allow Adding itemsRemoving itemsReferring to individual elementsCounting itemsClearing the collection
7-8
Filling a List/Using the Properties Window
Design time in Properties windowItems propertyClick on ellipses to open String
Collection EditorType list items, end each line with
Enter keyRun time methods
Items.Add--OR--
Items.Insert
7-9
Filling a List - Design Time
Click ellipses button to open
7-10
Using the Items.Add Method
Use to add new items to the list at run timeGeneral Form Examples Object.Items.Add(ItemValue)
Me.schoolsListBox.Items.Add("Harvard")Me.schoolsListBox.Items.Add("Stanford")Me.schoolsListBox.Items.Add(schoolsTextBox.Text)Me.majorsComboBox.Items.Add(majorsComboBox.Text)Me.majorsComboBox.Items.Add(majorString)
7-11
Using the Items.Insert Method
Use to add new items to the list at run time in a specific location (index position) in the collection
General Form
Examples
Me.schoolsListBox.Items.Insert(0, "Harvard")Me.majorsComboBox.Items.Insert(1, Me.majorsComboBox.Text)
Object.Items.Insert(IndexPosition, ItemValue)
7-12
The SelectedIndex Property
Index number of currently selected item is stored in the SelectedIndex property
If no list item is selected, SelectedIndex property is negative 1 (-1)
Use to select an item in list or deselect all items in code
7-13
The Items.Count Property
Use to determine number of items in the list
Remember: Items.Count is always one more than the highest possible Selected Index because indexes begin with 0For example, if there are five items in a list:
Items.Count = 5 ANDHighest Index = 4
7-14
Me.schoolsListBox.Items(5) = "University of California"
' Next line references the currently selected item.selectedFlavorString = flavorListBox.Items(flavorListBox.Selected Index).ToString( )
Referencing the Items Collection
Use the index of the item to reference a specific item in the collection
Remember that the index is zero based so the first item in the list is index position zero
7-15
Removing an Item from a List
Use the Items.RemoveAt method to remove an item by index from the list and the Items.Remove method to remove by specifying the text
General Form
ExamplesMe.namesListBox.Items.RemoveAt(0)‘ Remove the item in position indexInteger.Me.schoolsComboBox.Items.RemoveAt(indexInteger)‘ Remove the currently selected item.
Object.Items.RemoveAt(IndexPosition)
7-16
The Items.Remove Method
Use the Items.Remove method to remove an item by specifying the text
General Form
Examples
namesListBox.Items.Remove("My School")schoolsComboBox.Items.Remove(schoolTextBox.Text)' Next line removes the currently selected item.coffeeComboBox.Items.Remove(coffeeComboBox.Text)
Object.Items.Remove(TextString)
7-17
Clearing a List
Use the Items.Clear method to clear all items and empty a combo box or list box
General Form
Examples
Me.schoolsListBox.Items.Clear( )Me.majorsComboBox.Items.Clear( )
Object.Items.Clear( )
7-18
List Box and Combo Box Events
In the Editor window, select the control name in the Class Namelist (at the top-left of the window), drop down the Method Name list, and select the event for which you want to write code or double-click the event name in the Properties window after clicking the Events button
The Editor will create the procedure header for youTextChanged Event
Occurs when user types text into combo boxList box does not have TextChanged Event
Enter Event (control receives focus)-an Enter event fires when a user tabs from control to control
Leave Event (control loses focus)-a Leave event triggers a user tabs between controls
7-19
Do/Loops
A loop repeats a series of instructionsAn iteration is a single execution of the statement(s) in
the loopUsed when the exact number of iterations is unknownA Do/Loop terminates based on a specified condition
Execution of the loop continues while a condition is True or until a condition is True
The condition can be placed at the top or the bottom of the loop
7-20
The Do and Loop Statements -General Form
Do {While |Until} condition ' Statements in loop.
Loop
--OR--Do
' Statements in loop.Loop {While | Until} condition
Top of Loop Condition,
Pretest
Bottom of Loop
Condition,Posttest
7-21
Pretest vs. Posttest
Pretest, loop may never be executed since tested BEFORE running
Do While … LoopDo Until … Loop
Posttest, loop will always be executed at least onceDo … Loop WhileDo … Loop Until
7-22
Pretest vs. Posttest Diagram
7-23
The Boolean Data Type Revisited
Can help when searching a list for a specific valueBoolean variable is always in one of two states; True
or FalseWhen a particular situation occurs, set Boolean variable
to TrueUse a loop to check for True
Many programmers refer to Boolean variables as switches or flagsSwitches have two states – on or offFlags are considered either up or down
7-24
For/Next Loops
Used to repeat statements in a loop a specific number of times
Uses a numeric counter variable, called Loop Index, which is tested to determine the number of times the statements inside the loop will execute
Loop Index is incremented at the bottom of the loop on each iteration
Step value can be included to specify the incrementing amount to increment Loop Index, step can be a negative number
7-25
The For and Next Statements - General Form
For LoopIndex = InitialValue To TestValue [Step Increment] ' Statements in loop.
Next [LoopIndex]
A For/Next loop can handle all three elements of a counter-controlled loop
Initialize the counter
Increment the counter
Test the counter to determine when it is time to terminate the loop
7-26
For/Next Loop Diagram
7-27
Exiting Loops
In some situations you may need to exit the loop prematurely
Click on the form’s close box or use the VB menu bar or toolbar to stop the program; or Ctrl+Break
Use the Exit For statement inside the loop structureGenerally the Exit For statement is part of an If
statement
7-28
Making Entries Appear Selected
When a user tabs into a text box that already has an entry, the user-friendly approach is to select the text
If a text box fails validation, select the textSelecting the entry in a Text Box
Use the SelectAll methodGood location is in the text box’s Enter event
Selecting an entry in a List BoxSet the SelectedIndex property to make a single item in
a list box appear selected
7-29
Using Static Variables
Static local variables retain their value for the life of the project
Can be useful forRunning totalsRunning countsBoolean switchesStoring current page number/count when printing
multiple pages