The Items of a List View
In the previous sections, we saw how to visually create the items of a list view using the ListViewItem Collection Editor. The items of a list view are…
Adding an Item to the List View#
We have previously explored how to create list view items using the ListViewItem Collection Editor. These items are stored in the Items property, which is based on the ListView.ListViewItemCollection class. This class implements the IList, ICollection, and IEnumerable interfaces. To add new items programmatically, the ListViewItemCollection class provides the Add() method, available in three overloads. One version uses the following syntax: while keeping the original behavior intact.
public virtual ListViewItem Add(string text);This method expects a string that will display as the new item. Here is an example: while keeping the original behavior intact.
private void InitializeComponent()
{
Text = "Countries Statistics";
Size = new Size(452, 214);
lvwCountries = new ListView();
lvwCountries.Location = new Point(12, 12);
lvwCountries.Width = 420;
lvwCountries.Height = 160;
lvwCountries.Items.Add("Egypt");
Controls.Add(lvwCountries);
}This would produce:
As the Items property is in fact a list, each item of this collection is represented by the Item property of the ListViewItemCollection class. This Item property is based on the ListViewItem class. The ListViewItem class is equipped with various constructors, the default of which allows you to instantiate an item without giving much details. Instead of directly passing a string to the ListViewItemCollection.Add() method, you can first create a ListViewItem object and pass it to the following version of the ListViewItemCollection.Add() method:
public virtual ListViewItem Add(ListViewItem value);This method expects a ListViewItem value. One way you can use it consists of providing the string the item would display. To do this, you can use the following constructor of the ListViewItem class:
public ListViewItem(string text);The text for the new item to display is required as an argument for this constructor. An example is provided here:
private void InitializeComponent()
{
Text = "Countries Statistics";
Size = new Size(452, 214);
lvwCountries = new ListView();
lvwCountries.Location = new Point(12, 12);
lvwCountries.Width = 420;
lvwCountries.Height = 160;
lvwCountries.Items.Add("Egypt");
ListViewItem lviPortugal = new ListViewItem("Portugal");
lvwCountries.Items.Add(lviPortugal);
Controls.Add(lvwCountries);
}Practical Learning: Creating List View Items
- On the main menu, click Project -> Add Windows Form...
- Set the Name to AvailableProperty and click Add
- Design the form as follows: Control Text Name Other Properties Label Property #: TextBox txtPropertyNumber Modifiers: Public Button OK btnOK DialogResult: OK Button Cancel btnCancel DialogResult: Cancel Form FormBorderStyle: FixedDialog Text: Altair Realtors - Available Property StartPosition: CenterScreen AcceptButton: btnOK CancelButton: btnCancel MaximizeBox: False MinimizeBox: False ShowInTaskBar: False
- In the Solution Explorer, right-click Form1.cs and click Rename
- Type AltairRealtor.cs and press Enter twice
- On the form, double-click the New Property button and implement its event as follows: private void btnNewProperty_Click(object sender, EventArgs e) { AvailableProperty dlgProperty = new AvailableProperty(); Random rndNumber = new Random(DateTime.Now.Millisecond); int number1 = rndNumber.Next(100, 999); int number2 = rndNumber.Next(100, 999); string propNumber = number1 + "-" + number2; dlgProperty.txtPropertyNumber.Text = propNumber; dlgProperty.Text = "Altair Realtors - New Property"; if (dlgProperty.ShowDialog() == DialogResult.OK) { ListViewItem lviStoreItem = new ListViewItem(dlgProperty.txtPropertyNumber.Text); lvwProperties.Items.Add(lviStoreItem); } }
- Execute the application to test it
- When the form displays, click the New Property button and, when the dialog box comes up, click OK
- Do this five times to create five items
- Close the form and return to your programming environment
Adding an Array of Items#
You can use any of these techniques to create as many items as necessary. Alternatively, if you have many items to create, you can first store them in an array of ListViewItem values, then call the AddRange() method. The syntax of this method is:
public void AddRange(ListViewItem[] items);This method takes as argument an array of ListViewItem objects. Here is an example:
private void InitializeComponent()
{
Text = "Countries Statistics";
Size = new Size(452, 214);
lvwCountries = new ListView();
lvwCountries.Location = new Point(12, 12);
lvwCountries.Width = 420;
lvwCountries.Height = 160;
lvwCountries.Items.Add("Egypt");
ListViewItem lviPortugal = new ListViewItem("Portugal");
lvwCountries.Items.Add(lviPortugal);
ListViewItem[] lviCountries =
{
new ListViewItem("Australia"),
new ListViewItem("Mali"),
new ListViewItem("Sweden"),
new ListViewItem("Venezuela")
};
lvwCountries.Items.AddRange(lviCountries);
Controls.Add(lvwCountries);
}Alternatively, you can create an array of strings and pass it to the following constructor of the ListView class:
public ListViewItem(string[] items);Previous Copyright © 2008-2009 C# Key Next

