[問題] C# 刪除listbox中複數項的問題
※ [本文轉錄自 C_and_CPP 看板]
作者: tylerfirst (......) 看板: C_and_CPP
標題: [問題] C# 刪除listbox中複數項的問題
時間: Fri Oct 30 23:27:22 2009
如題,小弟用C#想設計一個button
此button按下的同時,會將listbox中選取的項目刪除
程式碼如下:
private void button5_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex != -1)
{
foreach (int select_Index in listBox1.SelectedIndices)
{
listBox1.Items.RemoveAt(select_Index);
}
}
}
跑出來的結果很奇怪= ="
如果選兩項,則只會刪除index比較前面的
如果選兩項以上,則會"隔項刪除".....意思就是選第一,二,四項卻只會刪掉一,四項
怎麼出現這種怪現象啊orz
我有試過把RemoveAt拿掉,單純用MessageBox顯示抓到的項數,結果是正常的
有想過會不會因為是Remove的關係導致程式抓的Index亂掉...
請板上前輩們指點一下 3Q<(_ _)>
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.173.164.170
→
10/30 23:37,
10/30 23:37
推
10/30 23:37,
10/30 23:37
→
10/30 23:40,
10/30 23:40
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.173.164.170
→
10/30 23:52, , 1F
10/30 23:52, 1F
→
10/30 23:52, , 2F
10/30 23:52, 2F
→
10/31 00:24, , 3F
10/31 00:24, 3F
→
10/31 00:24, , 4F
10/31 00:24, 4F
→
10/31 00:25, , 5F
10/31 00:25, 5F
→
10/31 00:28, , 6F
10/31 00:28, 6F
→
10/31 00:58, , 7F
10/31 00:58, 7F
→
10/31 08:55, , 8F
10/31 08:55, 8F