This documentation is archived and is not being maintained.

ListItemCollection.Clear Method

Removes all ListItem objects from the collection.

[Visual Basic]
Public Overridable Sub Clear() Implements IList.Clear
[C#]
public virtual void Clear();
[C++]
public: virtual void Clear();
[JScript]
public function Clear();

Implements

IList.Clear

Remarks

Use the Clear method to remove all ListItem objects. The Count property is also set to 0.

CAUTION   When you use this method, all ListItem objects are removed from the collection. You cannot undo this.

Example

[Visual Basic] 
' Copy the items in the ListListBox1.Items to an array before 
' deleting them.     
Dim myListItemArray(ListBox1.Items.Count - 1) As ListItem
ListBox1.Items.CopyTo(myListItemArray, 0)

' Delete all the items from the ListBox.
ListBox1.Items.Clear()
DeleteLabel.Text = "<b>All items in the ListBox were deleted successfully." + 

"</b><br><b>The deleted items are:"
Dim listResults As [String] = ""
Dim myItem2 As ListItem
For Each myItem2 In myListItemArray
    listResults = listResults & myItem2.Text & "<br>"
Next myItem2
ResultsLabel.Text = listResults

[C#] 
// Copy the items in the ListItemCollection to an array before 
// deleting them.     
ListItem[] myListItemArray = new ListItem[ListBox1.Items.Count];
ListBox1.Items.CopyTo(myListItemArray, 0);

// Delete all the items from the ListBox.
ListBox1.Items.Clear();
DeleteLabel.Text = "<b>All items in the ListBox were deleted successfully." 
    + "</b><br><b>The deleted items are:";
String listResults="";
    foreach(ListItem myItem in myListItemArray)
    {
        listResults = listResults + myItem.Text + "<br>";
    }
ResultsLabel.Text = listResults;

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

ListItemCollection Class | ListItemCollection Members | System.Web.UI.WebControls Namespace | Add | AddRange | Insert | Remove | RemoveAt

Show: