Michael
Michael

Reputation: 157

update a list after deleting an item

i have a form that when i add a item it stores it within a list and adds it to a checklistbox

i have a button that deletes the item from the checklist box, but how would i get it so that when i delete it from the checklistbox it also deletes it within the list its been stored in

here is the code for the delete button

    private void btnDelete_Click(object sender, EventArgs e)
    {
        for (int i = clbSummary.CheckedIndices.Count - 1; i >= 0; --i)
        {
            clbSummary.Items.RemoveAt(clbSummary.CheckedIndices[i]);
        }

    }

Upvotes: 0

Views: 201

Answers (2)

Zak Soliman
Zak Soliman

Reputation: 1170

Why don't you do remove the item from the list within the btnDelete_Click method.

For example:

private void btnDelete_Click(object sender, EventArgs e)
{
    for (int i = clbSummary.CheckedIndices.Count - 1; i >= 0; --i)
    {
        object item = clbSummary.Items[clbSummary.CheckedIndices[i]];
        myList = myList.Remove(item);
        clbSummary.Items.RemoveAt(clbSummary.CheckedIndices[i]);


    }

}

I'm not sure if you can use the [] operator on Items, but this is to give you a general idea.

Upvotes: 1

erictrigo
erictrigo

Reputation: 1017

Set the checklistbox DataSource property to the list where you are storing the items. When you make any changes to the list, your checklistbox will update.

Upvotes: 0

Related Questions