簡體   English   中英

如何使用刪除按鈕從ListView C#中刪除多個項目

[英]How To Delete Multiple Items from ListView C# With Delete Button

因此,我有一個簡單的ListView,用戶可以向其中添加信息,還有一個刪除按鈕,該按鈕只能一次刪除一個選定的項目。 我試圖做到這一點,以便在選擇多個項目並按下“刪除”時,它會刪除那些選定的項目,而不僅僅是一個。 感謝您的幫助!

添加收件人代碼:

 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();
        }
    }

我的刪除按鈕代碼(目前僅刪除一個選擇)

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

        catch { }
        UpdateNoOfEmails();
    }

清除所有收件人代碼

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

就我而言,最簡單的方法是使用while循環。 這是我新的刪除按鈕代碼的樣子:

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

        catch { }
        UpdateNoOfEmails();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM