C# 如何测试在c中使用for从列表框中删除了多少项# If((textbox1.text-textbox2.text)==100 | |(textbox1.text-text2.text)==250) { 用于(长i=textbox2.text;i

C# 如何测试在c中使用for从列表框中删除了多少项# If((textbox1.text-textbox2.text)==100 | |(textbox1.text-text2.text)==250) { 用于(长i=textbox2.text;i,c#,C#,在我开始回答之前,我假设您在问题中包含的代码如下: If ((textbox1.text - textbox2.text)==100 ||(textbox1.text - text2.text)==250) { For (long i= textbox2.text;i < textbox1.text ;i++) { Listbox.remove (i)//listbox contain serials // how can test

在我开始回答之前,我假设您在问题中包含的代码如下:

If ((textbox1.text - textbox2.text)==100 ||(textbox1.text - text2.text)==250)
{
   For (long i= textbox2.text;i < textbox1.text ;i++)
   {
           Listbox.remove (i)//listbox contain serials 
           // how can test how many items had been deleted form the listbox
   }
}
您可以在执行删除操作的循环中添加一个“计数器”

例如:

int tb1 = Convert.ToInt32(textbox1.text);
int tb2 = Convert.ToInt32(textbox2.text);
if ((tb1 - tb2)==100 ||(tb1 - tb2)==250)
{
    int counter=0;
    for (long i= tb2;i < tb1;i++)
    {
        if (Listbox.Items.Contains(i)) {
            counter++;
            Listbox.Items.Remove(i);
        }
    }
}
Console.WriteLine(counter + " items were removed");
int计数器=0;
//比方说,这是您删除列表框上项目的循环

对于(int i=0;i请从VS IDE复制代码,而不是手工编写。您的代码完全不起作用。也许您可以尝试编写一些代码,看看您能做多少。可能是因为您不能在算术运算中使用字符串,这是第一行中的一个问题,我没有寻找它,因为它不是原始问题的一部分,它是edited现在。你是如何将tb1声明为一个字符串并赋予它一个int的?更不用说大写的
If
int
For
…我可以看出你的代码并不是通过查看它来编译的。抱歉,我身上没有编译器来验证语法等。你提出的问题已经解决了
int tb1 = Convert.ToInt32(textbox1.text);
int tb2 = Convert.ToInt32(textbox2.text);
if ((tb1 - tb2)==100 ||(tb1 - tb2)==250)
{
    int counter=0;
    for (long i= tb2;i < tb1;i++)
    {
        if (Listbox.Items.Contains(i)) {
            counter++;
            Listbox.Items.Remove(i);
        }
    }
}
Console.WriteLine(counter + " items were removed");
      int counter = 0;

      //say, this is your loop for deletion of items on listbox
      for (int i=0; i<5; i++)
      {
          //if condition for deletion was met
          counter++;
      }

      //you can call counter after the loop, this will be the number of deleted items from your listbox