在 C# 中从 ListView 中删除项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15572169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:14:18  来源:igfitidea点击:

Delete Items from ListView in C#

c#listview

提问by mabezat

I need to delete items from a ListView, the code I am looking for will show a MessageBox to confirm and if no item is selected it will show an error MessageBox

我需要从 a 中删除项目ListView,我正在寻找的代码将显示一个 MessageBox 来确认,如果没有选择任何项目,它将显示一个错误 MessageBox

This is my code and it is not working :(

这是我的代码,它不起作用:(

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

The error is not in delete but, in MessageBox'sI have two MessageBox's, error must be shown first before confirmation.

错误不是在删除中,而是在MessageBox's我有两个中MessageBox's,在确认之前必须先显示错误。

采纳答案by Steve

Start counting from the end going to zero

从最后到零开始计数

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

However consider that every ListViewItem has an Index property and using that collection has the advantage to avoid a redundant test and looping on a lesser number of items.

但是考虑到每个 ListViewItem 都有一个 Index 属性,并且使用该集合具有避免冗余测试和在较少数量的项目上循环的优势。

(Note, the SelectedItems collection is never null, if no selection is present, then the collection is empty but not null)

(注意,SelectedItems 集合永远不会为空,如果不存在选择,则该集合为空但不为空)

So your code could be rewritten

所以你的代码可以被重写

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", ...);

回答by Aghilas Yakoub

You can use just this code without -- decrement

您可以只使用此代码而无需 -- 递减

listView1.Items[i].Remove();

Note : You can also use RemoteAt methodby specifing position

注意:您也可以RemoteAt method通过指定位置使用

回答by Thelonias

You need to change your confirmation MessageBoxfrom Showto ShowDialog. This will make it modal and wait for a result.

您需要将您的确认MessageBox从更改ShowShowDialog。这将使其成为模态并等待结果。

You need to check for emptry on "SelectedItems"

您需要检查“SelectedItems”是否为空

回答by Nikola Davidovic

You can change the code like this. Note that ListView.SelectedIndicescollection holds the indexes of the selected ListViewItems. Just iterate them from the end towards beginning and you won't need to deal with index updates but leave them to the forloop:

您可以像这样更改代码。请注意,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);

回答by Sinatr

You should not reference the original collection you are using during iteration, but some other:

你不应该引用你在迭代过程中使用的原始集合,而是一些其他的:

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

回答by Sinatr

//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]);
                    }
                }
            }