删除与之间的区别在列表框中删除

时间:2013-03-25 23:05:49

标签: c# listbox

Remove&之间有什么区别? RemoveAt 中的ListBox

2 个答案:

答案 0 :(得分:7)

首先,如果您有ListBox listBox;,那么listBox没有方法RemoveRemoveAt。它将是listBox.Items.Remove(...)listBox.Items.RemoveAt(...)。我在此假设您使用ListBox中的System.Windows.Forms

现在,RemoveRemoveAt之间的区别在于一个要从列表中删除项目,而一个需要一个索引

为了更清楚,让我们创建一个List<int> list = new List<int>(new int[] { 10, 20, 30, 40 });。由于在C#中的所有内容都是零基础,因此索引0处的列表中的值为10,索引1处的值为20等。

ListObjectCollection一样,具有RemoveRemoveAt方法。对于我们的简单列表,调用list.Remove(20);将删除它在列表中找到的20的第一个出现位置。 <{1}}自list被删除后,{ 10, 30, 40 }将结束{。}}。

如果不是在20上调用Remove,而是调用list,它会对列表执行相同的操作。我们正在索引list.RemoveAt(1);删除列表的元素:在这种情况下,1

答案 1 :(得分:0)

我正在计算RemoveAt和Remove函数之间的差异。检出以下代码段:

       {
           while(ShowListBox.Items.Count != 0)
           {

               for(int i = 0; i < ShowListBox.Items.Count; i++)
               {
                   ShowListBox.Items.Remove(ShowListBox.Items[i].ToString());
               }
               while (ShowListBox.Items.Count > 0)
               {
                   ShowListBox.Items.RemoveAt(0);
               }

           }

       }

在上面的代码中,所有数据均被删除(文本,数字和列表框中的任何符号)

使用“删除”功能将其删除时失败。