2013-03-22 96 views
4

我需要從列表視圖中刪除的項目,我要找的代碼會顯示一個消息框,確認,如果沒有選中的項,它會顯示一個錯誤消息框刪除項目在C#

這是我的代碼,它不工作:(

private void button2_Click(object sender, EventArgs e) 
{ 
    if (listView1.SelectedItems != null) 
    { 
     var confirmation = MessageBox.Show(
      "Voulez vous vraiment supprimer les stagiaires séléctionnés?", 
      "Suppression", MessageBoxButtons.YesNo, MessageBoxIcon.Question 
     ); 

     if (confirmation == DialogResult.Yes) 
     { 
      for (int i = 0; i < listView1.Items.Count; i++) 
      { 
       if (listView1.Items[i].Selected) 
       { 
        listView1.Items[i].Remove(); 
        i--; 
       } 
      } 
     } 
    } 
    else 
    { 
     MessageBox.Show("aucin stagiaire selectionnes", "erreur", 
      MessageBoxButtons.OK, MessageBoxIcon.Error); 
    } 
} 

的錯誤是不刪除,但在messageboxs我有兩個messagesbox,ERREUR必須首先顯示確認之前。

+0

您是否獲得在輸出窗口中的任何錯誤? – Thelonias 2013-03-22 14:03:07

+0

你會得到什麼例外? – 2013-03-22 14:03:41

+0

我沒有得到錯誤,也沒有例外,我實際上可以刪除項目,但是,錯誤是在messageboxs, 非常感謝:) – mabezat 2013-03-22 14:05:32

回答

8

開始從最終要計數零

for (int i = listView1.Items.Count - 1; i >= 0; i--) 
{ 
    if (listView1.Items[i].Selected) 
    { 
     listView1.Items[i].Remove(); 
    } 
} 

但是,考慮到每個ListViewItem都有一個Index屬性,並且使用該集合有利於避免冗餘測試並在較少數量的項目上循環。

(注意,該SelectedItems集合永遠不能爲null,如果沒有選擇存在,則集合爲空,但不是null)

所以,你的代碼可以改寫

if (listView1.SelectedItems.Count > 0) 
{ 
    var confirmation = MessageBox.Show("Voulez vous vraiment supprimer les stagiaires séléctionnés?", "Suppression", MessageBoxButtons.YesNo, MessageBoxIcon.Question); 
    if (confirmation == DialogResult.Yes) 
    { 
     for (int i = listView1.SelectedItems.Count - 1; i >= 0; i--) 
     { 
      ListViewItem itm = listView1.SelectedItems[i]; 
      listView1.Items[itm.Index].Remove(); 
     } 
    } 
} 
else 
    MessageBox.Show("aucin stagiaire selectionnes", ...); 
+0

錯誤不是在刪除,但在messageboxs我有兩個messagebox,錯誤必須先確認之前顯示 – mabezat 2013-03-22 14:09:09

+2

是的,因爲你測試爲null ,而不是SelectedItems集合從不爲空,如果沒有選擇存在它是一個空集合 – Steve 2013-03-22 14:10:01

+0

好吧,那麼我該如何解決這個問題?請 – mabezat 2013-03-22 14:13:26

0

你可以只使用這個代碼不 - 遞減

listView1.Items[i].Remove(); 

注意:您還可以通過specifing位置

使用
0

您需要將確認MessageBoxShow更改爲ShowDialog。這將使其變爲模態並等待結果。

您需要檢查emptry的「SelectedItems」

+0

我得到這個錯誤(錯誤'System.Windows.Forms。MessageBox'不包含'ShowDialog'的定義\t) – mabezat 2013-03-22 14:11:25

+0

噢,好的,我以爲你在使用WPF。 – Thelonias 2013-03-22 14:13:49

0

你可以改變這樣的代碼。請注意,ListView.SelectedIndices集合包含所選ListViewItems的索引。剛剛從開始走向結束迭代他們,你會不會需要處理的索引更新,但他們留給for循環:

  if (listView1.SelectedIndices.Count>0) 
      { 
       var confirmation = MessageBox.Show("Voulez vous vraiment supprimer les stagiaires séléctionnés?", "Suppression", MessageBoxButtons.YesNo, MessageBoxIcon.Question); 
       if (confirmation == DialogResult.Yes) 
       { 
        for (int i = listView1.SelectedIndices.Count-1; i >= 0; i--) 
        { 

         listView1.Items.RemoveAt(listView1.SelectedIndices[i]); 

        } 
       } 
      } 
      else 
       MessageBox.Show("aucin stagiaire selectionnes", "erreur", MessageBoxButtons.OK, MessageBoxIcon.Error); 
1

你不應該引用您的迭代過程中使用的是原來的集合,但其他一些:

  foreach(ListViewItem item in listView1.Items) 
       if (item.Selected) 
        listView1.Items.Remove(item); 
+0

這也工作,謝謝你 – mabezat 2013-03-22 14:20:01

+2

你不能修改集合,而枚舉它 – Thelonias 2013-03-22 14:20:58

0
//if (lvPhotos.SelectedIndices.Count > 0) 
      if (lvPhotos.CheckedIndices.Count > 0) 
      { 
       var confirmation = MessageBox.Show("Supprimer les photos séléctionnées ?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question); 
       if (confirmation == DialogResult.Yes) 
       { 
        // selected 
        //for (int i = lvPhotos.SelectedIndices.Count - 1; i >= 0; i--) 
        //{ 
        // lvPhotos.Items.RemoveAt(lvPhotos.SelectedIndices[i]); 
        //} 

        // checked 
        for (int i = lvPhotos.CheckedIndices.Count - 1; i >= 0; i--) 
        { 
         lvPhotos.Items.RemoveAt(lvPhotos.CheckedIndices[i]); 
        } 
       } 
      }