从C#中的ListView中删除项目
我需要从列表视图中删除项目,我要查找的代码将显示一个消息框以确认,如果没有选择项目,它将显示一个错误消息框
这是我的代码,它不起作用:(
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); } }
该错误不在删除中,但在消息框中我有两个消息框,必须首先显示erreur才能确认。
从最后开始计数到零
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", ...);
您不应该引用在迭代期间使用的原始集合,而是其他一些:
foreach(ListViewItem item in listView1.Items) if (item.Selected) listView1.Items.Remove(item);
您可以使用此代码而不减少
listView1.Items[i].Remove();
注意:您还可以通过RemoteAt method
位置来使用RemoteAt method
您可以像这样更改代码。 请注意, 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);
您需要将确认MessageBox
从Show
更改为ShowDialog
。 这将使其成为模态并等待结果。
你需要检查“SelectedItems”上的emptry
上述就是C#学习教程:从C#中的ListView中删除项目分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
//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]); } } }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1018238.html