Tony
Tony

Reputation: 507

listbox and how to perform actions on selected items

Hi I have this program Im trying to make

but now Im trying to add an option to delete items ( only the selected ones ).

thanx in advance

Upvotes: 1

Views: 948

Answers (2)

skid
skid

Reputation: 137

protected void Button2_Click(object sender, EventArgs e)
{
    for(int i = ListBox1.Items.Count -1; i>=0; i--)
    {
        if (ListBox1.Items[i].Selected)
        {
            ListBox1.Items.Remove(ListBox1.Items[i]);
        }
    }
}

This should work for deleting also

Upvotes: 0

Jon Seigel
Jon Seigel

Reputation: 12401

To delete the selected items:

while (listBox1.SelectedIndices.Count > 0)
    listBox1.Items.RemoveAt(listBox1.SelectedIndices[0]);

Upvotes: 1

Related Questions