0

So, I have a simple ListView that users can add information to and a delete button that is only capable of deleting one selected item at a time. I'm trying to make it so that when multiple items are selected and 'delete' is pressed it deletes those selected items instead of just one. Your help is appreciated!

Add recipient code:

 private void addtoRecipients_Click(object sender, EventArgs e)
    {
        if (recipientEmailBox.Text != "")
        {
            string[] S = new string[4];
            S[0] = recipientEmailBox.Text;
            S[1] = recipientNameBox.Text;
            S[2] = txtLocation.Text;
            S[3] = txtSubject.Text;
            ListViewItem I = new ListViewItem(S);
            recipientBox.Items.Add(I);
            UpdateNoOfEmails();
        }
    }

My Delete Button Code (only deletes one selection at the moment)

 private void deleteEntryBTN_Click(object sender, EventArgs e)
    {
        try { recipientBox.Items.Remove(recipientBox.SelectedItems[0]); }

        catch { }
        UpdateNoOfEmails();
    }

Clear All Recipients Code

private void clearBTN_Click(object sender, EventArgs e)
    {
        recipientBox.Items.Clear();
        UpdateNoOfEmails();
    }

1 Answer 1

1

In my case, the simplest way to do it was with a while loop. This is what my new delete button code looks like:

 private void deleteEntryBTN_Click(object sender, EventArgs e)
    {
        try
        {
            while (recipientBox.SelectedItems.Count > 0)
            {
                recipientBox.Items.Remove(recipientBox.SelectedItems[0]);
            }
        }

        catch { }
        UpdateNoOfEmails();
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.