8.12 Adding and Removing Items in a ListBox Control

 <  Day Day Up  >  

8.12 Adding and Removing Items in a ListBox Control

You want to create a list of items and programmatically add and remove items from the list.


Technique

Use the ListBox control if you want to display a simple list. To add a list of items using the form designer, select the ListBox control and click the browse button, which you see when you select the Items property. This move displays the string collection editor. Add new items to the ListBox by inserting strings individually on separate lines.

You can also programmatically add items by calling the Add method defined in the Items collection of an existing ListBox object. This method accepts a System.Object parameter, which means you are free to add any type of object to the collection. When the object appears in the ListBox , its ToString representation appears:

 
 foreach( string selectedName in lbContacts.SelectedItems ) {     lbRecip.Items.Add( selectedName ); } 

You can also add an array of objects to the ListBox by calling the AddRange method on the Items collection. The following code copies all the items from one ListBox control to another:

 
 lbRecip.Items.AddRange( lbContacts.Items ); 

To remove an item from a ListBox , call the Remove method defined in the Items collection. The Remove method uses the properties of object equality to find the object within the collection. For instance, using string objects, it looks for the item with the same string value:

 
 lbContacts.Items.Remove( "Doug Deprenger" ); 

You can also remove an item by its position by calling the RemoveAt method. This method uses the zero-based index of the item in the collection as its parameter. To remove all items in a ListBox , call the Clear method:

 
 lbContacts.Items.RemoveAt( 0 ); lbContacts.Items.Clear(); 

Comments

A ListBox is useful if you want to display a list of strings to the user and give the user the ability to select the strings to perform some action. This list, for instance, could be a list of contact names or items on a shopping list. The items in a ListBox are contained within the Items collection, which contains the necessary methods to add and remove items. When you add a string to the Items collection, the ListBox is updated and the new item string representation appears.

A ListBox supports a few different selection methods. To specify the allowable selection a user can make, change the SelectionMode property using a value from the SelectionMode enumerated data type. If the selection mode is None , then the user will be unable to select an item in the list. When you set SelectionMode to single, then the list items are mutually exclusive, which means that when one item is selected, the previous item is unselected . You can also use a multiple-selection list box by specifying a selection mode of MultiSimple or MultiExtended . The difference between the two involves the use of the Ctrl key on the keyboard. With a MultiExtended style, the user must hold down the Ctrl key to select multiple items. The MultiSimple does not require the use of the Ctrl key, and the user selects or deselects items simply by clicking on them. In a multiple-selection list box, you can determine which items are selected by accessing the SelectedItems or SelectedIndices collection. In a single-selection list box, you can reference the SelectedItem or SelectedIndex properties.

In Listing 8.5, you can see event handlers for the five buttons shown in Figure 8.4. In this application, you can move names between the two list boxes. The methods btnSingleAdd_Click and btnSingleRemove_Click enumerate the selected items and move them to the corresponding list box. The event handlers btnAddAll_Click and btnRemoveAll_Click use the ListBox methods AddRange and Clear to add or remove an entire group of items from one list box to the other.

Listing 8.5 Moving Items Between Two Multiple-Selection ListBox Objects
 private void btnSingleAdd_Click(object sender, System.EventArgs e) {     // enumerate selected items     foreach( string selectedName in lbContacts.SelectedItems )     {         // add item to other list box         lbRecip.Items.Add( selectedName );     }     // create a copy of all contacts items     // this is done since an exception is thrown if you     // add items to a collection while that     //    collection is being enumerated within a foreach loop     ListBox.ObjectCollection newContacts =         new ListBox.ObjectCollection( lbContacts, lbContacts.Items );     foreach( string selectedName in lbContacts.SelectedItems )     {         // remove the item from the temporary collection         newContacts.Remove( selectedName );     }     // clear all items and insert items from temporary collection     lbContacts.Items.Clear();     lbContacts.Items.AddRange( newContacts ); } private void btnAllAdd_Click(object sender, System.EventArgs e) {     // add all items from contacts to recipient's listbox     lbRecip.Items.AddRange( lbContacts.Items );     // clear contacts list box     lbContacts.Items.Clear(); } private void btnSingleRemove_Click(object sender, System.EventArgs e) {     foreach( string selectedName in lbRecip.SelectedItems )     {         lbContacts.Items.Add( selectedName );     }     ListBox.ObjectCollection newRecip =         new ListBox.ObjectCollection( lbRecip, lbRecip.Items );     foreach( string selectedName in lbRecip.SelectedItems )     {         newRecip.Remove( selectedName );     }     lbRecip.Items.Clear();     lbRecip.Items.AddRange( newRecip ); } private void btnAllRemove_Click(object sender, System.EventArgs e) {     lbContacts.Items.AddRange( lbRecip.Items );     lbRecip.Items.Clear(); } private void btnNew_Click(object sender, System.EventArgs e) {     // Note: NewContact class not shown     NewContact contactForm = new NewContact();     contactForm.ShowDialog();     // add new contact to contact list     lbContacts.Items.Add( contactForm.ContactName ); } 
Figure 8.4. You can move multiple items from one ListBox to the other.

graphics/08fig04.gif

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net