Bewerken

Share via


ListItemCollection.Remove Method

Definition

Removes a ListItem from the collection.

Overloads

Remove(String)

Removes the ListItem that's represented by the specified string from the collection.

Remove(ListItem)

Removes the specified ListItem from the collection.

Remove(String)

Removes the ListItem that's represented by the specified string from the collection.

public:
 void Remove(System::String ^ item);
public void Remove (string item);
member this.Remove : string -> unit
Public Sub Remove (item As String)

Parameters

item
String

A String that represents the item to remove from the collection.

Examples

The following example demonstrates the Remove method of the ListItemCollection class. The Web page contains a ListBox control with some list items in it and a TextBox control named Delete. The user enters the text of the item to delete into the TextBox control. The Click event handler of the Button1 control deletes the selected item from the ListItemCollection object and therefore from the ListBox control.

ListItem myListItem = new ListItem(Delete.Text.ToLower(),Delete.Text.ToLower());
// Check whether the 'ListItem' is present in the 'ListBox' or not.
if(ItemCollection.Contains(myListItem))
{
   String deleteString=Delete.Text;
   // Delete the listitem entered by the user in textfield.
   ItemCollection.Remove(deleteString.ToLower());
   Message.Text="<font color='green'><b>Deleted Successfully</b></font>";
}
else
{
  Message.Text="<font color='red'><b>No ListItem with the given name is present in the ListBox for deletion.</b></font>";
}               

     Dim myListItem As ListItem = new ListItem(Delete.Text.ToLower(),Delete.Text.ToLower())
     ' Check whether the 'ListItem' is present in the 'ListBox' or not.
     If(ItemCollection.Contains(myListItem)) Then
     
        Dim deleteString As String =Delete.Text
        ' Delete the listitem entered by the user in textfield.
        ItemCollection.Remove(deleteString.ToLower())
        Message.Text="<font color='green'><b>Deleted Successfully</b></font>"
     Else
     
       Message.Text="<font color='red'><b>No ListItem with the given name is present in the ListBox for deletion.</b></font>"
     End If

Remarks

Use the Remove method to remove a ListItem object from a collection. This method creates a ListItem object using the item parameter text and then removes that ListItem from the collection. The specified item text must match the Value property and the text of an existing ListItem object exactly; otherwise, no item is removed.

See also

Applies to

Remove(ListItem)

Removes the specified ListItem from the collection.

public:
 void Remove(System::Web::UI::WebControls::ListItem ^ item);
public void Remove (System.Web.UI.WebControls.ListItem item);
member this.Remove : System.Web.UI.WebControls.ListItem -> unit
Public Sub Remove (item As ListItem)

Parameters

item
ListItem

The ListItem to remove from the collection.

Examples

The following code example demonstrates creating ListItemCollection objects, adding items to the collections, and removing items from the collections. In the example, the ListItemCollection named listBoxData is used as the data source for a ListBox control called ListBox1, and the ListItemCollection called ddBoxData is used as the data source for a DropDownList control called DropDownList1. To view this code in a fully working example, see the WebControl class topic.

//Set the SelectedIndex to -1 so no items are selected.
// The new item will be set as the selected item when it is added.
DropDownList1.SelectedIndex = -1;
// Add the selected item to DropDownList1.
DropDownList1.Items.Add(ListBox1.SelectedItem);
// Delete the selected item from ListBox1.
ListBox1.Items.Remove(ListBox1.SelectedItem);
' Add the selected item to DropDownList1.
DropDownList1.Items.Add(ListBox1.SelectedItem)
' Delete the selected item from ListBox1.
ListBox1.Items.Remove(ListBox1.SelectedItem)

Remarks

Use the Remove method to remove a ListItem from the collection. This implementation of the method takes the ListItem specified by the item parameter and removes it from the collection.

See also

Applies to