8.13 Using a ListView Control

 <  Day Day Up  >  

8.13 Using a ListView Control

You want to add and remove items from a ListView control.


Technique

As with the ListBox control, you manipulate a ListView 's items using the Items collection. The Add method of the Items collection contains three overloaded methods . You can add an item just by passing a string object:

 
 lvAvailable.Items.Add( "Normal" ); 

To allow more customizability of a ListView item, create a ListViewItem object and set its properties accordingly . Some of the properties include color properties and check states:

 
 ListViewItem listViewItem1 = new ListViewItem( "Happy" ); listViewItem1.ForeColor = Color.Red; lvAvailable.Items.Add( listViewItem1 ); 

You can also add a range of ListViewItem objects by creating an array and calling the AddRange method of the Items collection:

 
 // adding an array of ListViewItems ListViewItem[] lvItems = new ListViewItem[]{ new ListViewItem("Sad"),                                              new ListViewItem("Angry"),                                              new ListViewItem("Surprised"),                                              new ListViewItem("Bashful")}; lvAvailable.Items.AddRange( lvItems ); 

Removing ListView items is similar to removing items from a ListBox control, which is demonstrated in the previous recipe. However, the Remove method only allows removal using a ListViewItem object. To retrieve ListViewItem objects, you can access the SelectedItems collection or use the Items indexer:

 
 private void btnSingleRemove_Click(object sender, System.EventArgs e) {     foreach( ListViewItem selectedEmoticon in lvChosen.SelectedItems )     {         lvAvailable.Items.Add( (ListViewItem) selectedEmoticon.Clone() );     }     foreach( ListViewItem lvItem in lvChosen.SelectedItems )         lvChosen.Items.Remove( lvItem ); } 

Comments

A ListView control is similar to a ListBox , but it allows you to display items in a variety of different view modes. The ListView works with a specially designed data type rather than a generic object used by a ListBox . The ListViewItem class contains several properties, allowing you to individually alter the appearance of individual items within a ListView . Adding new items, however, is still similar to that of a ListBox because both controls contain an Items collection with the main difference being the data type stored in that collection. The ListViewItems editor, which is invoked through the Items property in the property browser, contains more information and available properties for each item than the simple string collection editor used by the ListBox control. The next few recipes go into detail about manipulating the ListView control and its individual items.

 <  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