2016-05-16 176 views
0

所以,我有一個簡單的ListView,用戶可以添加信息和刪除按鈕,只能刪除一次選定的項目。我試圖做到這一點,當多個項目被選中,'刪除'被按下時,它會刪除那些選定的項目,而不是一個。感謝您的幫助!如何從ListView C#刪除多個項目與刪除按鈕

添加收件人代碼:

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

回答

0

在我的情況下,最簡單的方法是使用while循環。這是我的新刪除按鈕代碼的樣子:

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

     catch { } 
     UpdateNoOfEmails(); 
    } 
相關問題