Export (0) Print
Expand All

Adding and Removing Items with the Windows Forms ListView Control

Visual Studio .NET 2003

The process of adding an item to a Windows Forms ListView control consists primarily of specifying the item and assigning properties to it. Adding or removing list items can be done at any time.

To add or remove items in the designer

  1. In the Properties window, click the ellipsis button (Aa983548.vbellipsesbutton(en-us,VS.71).gif) next to the Items property.

    The ListViewItem Collection Editor appears.

  2. To add an item, click the Add button. You can then set properties of the new item, such as the Text and ImageIndex properties. To remove an item, select it and click the Remove button.

To add items programmatically

  • Use the Add method of the Items property.
    ' Visual Basic
    ' Adds a new item with ImageIndex 3
    ListView1.Items.Add("List item text", 3)
    
    // C#
    // Adds a new item with ImageIndex 3
    listView1.Items.Add("List item text", 3);
    
    // C++
    // Adds a new item with ImageIndex 3
    listView1->Items->Add(S"List item text", 3);
    

To remove items programmatically

  • Use the RemoveAt or Clear method of the Items property. The RemoveAt method removes a single item; the Clear method removes all items from the list.
    ' Visual Basic
    ' Removes the first item in the list.
    ListView1.Items.RemoveAt(0)
    ' Clears all items:
    ListView1.Items.Clear()
    
    // C#
    // Removes the first item in the list.
    listView1.Items.RemoveAt(0);
    // Clears all the items.
    listView1.Items.Clear();
    
    // C++
    // Removes the first item in the list.
    listView1->Items->RemoveAt(0);
    // Clears all the items.
    listView1->Items->Clear();
    

See Also

Introduction to the Windows Forms ListView Control | Adding Columns to the Windows Forms ListView Control | Displaying Subitems in Columns with the Windows Forms ListView Control | Displaying Icons for the Windows Forms ListView Control | Adding Custom Information to a TreeView or ListView Control (Windows Forms)

Show:
© 2014 Microsoft