Chapter 7 Lists, Loops, and PrintingList Boxes and Combo BoxesSlide 3Slide 4Slide 5List Boxes and Combo Boxes cont.The Items CollectionFilling a ListPowerPoint PresentationSlide 10Using the Items.Add MethodUsing the Items.Insert MethodThe SelectedIndex PropertyThe Items.Count PropertyReferencing the Items CollectionReferencing the Items Collection cont.Removing an Item from a ListClearing a ListList Box and Combo Box EventsThe Three Programming StructuresThe while and do/while LoopsThe while and do/while Loops cont.Slide 23Slide 24The bool Data Type Revisitedfor Loopsfor Loops cont.Slide 28Making Entries Appear SelectedSending Information to the PrinterThe PrintDocument ControlSetting Up the Print OutputPrintDocument ControlPrintDocument Control EventsThe Graphics PageUsing the DrawString MethodSetting the X and Y CoordinatesPrinting the Contents of a List BoxPrinting cont.Printing Multiple PagesChapter 7Lists, Loops, and PrintingProgramming in C# .NETProgramming in C# .NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.2List Boxes and Combo BoxesBoth can be used for user to select from a Both can be used for user to select from a list of itemslist of itemsCombo box has the additional capability Combo box has the additional capability of allowing the user to type into a text boxof allowing the user to type into a text box3List Boxes and Combo BoxesTypes of list boxes and combo boxesTypes of list boxes and combo boxesSimple list boxesSimple list boxesCan only choose one itemCan only choose one itemChecked list boxes Checked list boxes Can select multiple itemsCan select multiple itemsSimple combo boxesSimple combo boxesHas a text box where user can type itemHas a text box where user can type itemDrop-down combo boxesDrop-down combo boxesLike the simple list but user can type text into a text boxLike the simple list but user can type text into a text boxDrop-down listsDrop-down listsNo text box displayed at top user cannot type new textNo text box displayed at top user cannot type new text4List Boxes and Combo Boxes5List Boxes and Combo BoxesList boxes and combo boxes have many common List boxes and combo boxes have many common properties and operate similarlyproperties and operate similarlyCombo box control has a Combo box control has a DropDownStyle propertyDropDownStyle property to determine if list box has a text box for user entry to determine if list box has a text box for user entry and whether list will drop downand whether list will drop downDrop-Down and Simple Combo style have text Drop-Down and Simple Combo style have text boxesboxesDrop-List has no text boxDrop-List has no text box6List Boxes and Combo Boxes cont.Scroll bars are added automatically if the box is too Scroll bars are added automatically if the box is too small to display all items in listsmall to display all items in listSelect list style based on Select list style based on space available space available how box should operatehow box should operateList box displays Name property in controlList box displays Name property in controlCombo box displays Text property in controlCombo box displays Text property in controlList box Text property available only at run timeList box Text property available only at run time7The Items CollectionList of items in a list box or combo box is a List of items in a list box or combo box is a collectioncollectionCollections are objects with properties and Collections are objects with properties and methodsmethodsItems collectionItems collection used to add items, remove items, used to add items, remove items, refer to items, count the items, and clear the listrefer to items, count the items, and clear the listRefer to items in collection by an indexRefer to items in collection by an index which which starts at zerostarts at zeroRefer to first item in the Items collection as Refer to first item in the Items collection as Items[0]Items[0]8Filling a ListUse Items property at design time if list Use Items property at design time if list contents are known and will not changecontents are known and will not changeClick on ellipses button next to Items Click on ellipses button next to Items property to open String Collection Editorproperty to open String Collection EditorUse String Collection Editor to add items to Use String Collection Editor to add items to listlistTo add items during execution, use the To add items during execution, use the Items.AddItems.Add or or Items.InsertItems.Insert method method91011Using the Items.Add MethodGeneral FormGeneral FormObject.Items.Add(ItemValue);Object.Items.Add(ItemValue);New item added to end of the listNew item added to end of the listIf the control’s If the control’s SortedSorted property set to property set to truetrue places places item alphabetically in listitem alphabetically in listMust use the Must use the AddAdd method to add value user types in method to add value user types in text box of combo box to the listtext box of combo box to the listExample: Example: cboCoffee.Items.Add(cboCoffee.Text);cboCoffee.Items.Add(cboCoffee.Text);12Using the Items.Insert MethodChoose location for a new item in the list by Choose location for a new item in the list by specifying the index positionspecifying the index positionGeneral Form General Form Object.Items.Insert(IndexPosition, ItemValue);Object.Items.Insert(IndexPosition, ItemValue);Index is zero basedIndex is zero basedDo not set the list control’s Sorted property to Do not set the list control’s Sorted property to truetrue if if using the Insert methodusing the Insert methodExample: Example: lstSchools.Items.Insert(0,”Harvard”)lstSchools.Items.Insert(0,”Harvard”);;13The SelectedIndex PropertySelectedIndexSelectedIndex property property contains index number contains index number of item the user selects (highlights)of item the user selects (highlights)If SelectedIndex is set to negative 1 (-1)If SelectedIndex is set to negative 1 (-1)Example: Example: lstCoffeeTypes.SelectedIndex = 3;lstCoffeeTypes.SelectedIndex = 3; //Select fourth item in list //Select fourth item in list14The Items.Count PropertyCountCount property property of Items collection contains of Items collection contains number of items in the listnumber of items in the listItems.Count is always one more than the Items.Count is
View Full Document