2017-04-19 34 views
-5
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 
    } 
} 
+2

請從VS IDE,而不是手寫的複製代碼。 –

+1

您的代碼完全無法使用。也許你可以嘗試編寫一些代碼,看看你得到了多少。 – Corey

回答

0

在我開始回答,我假設你在你的問題包括代碼如下列表框:

If ((textbox1.text - textbox2.text)==100 ||(textbox1.text - text2.text)==250) 
{ 
    For (Int 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 
    } 
} 

如果您在使用標準列表框,正確的語法來刪除項目從它是這樣的:Listbox.Items.Remove(item)docs

此外,你不能在算術運算使用textbox.text,您需要將字符串轉換爲整數首先使用Convert.Toint32

要計算受影響的項目的數量,只需檢查他們是否存在,您移除它們之前,並創建一個計數器變量:

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"); 
+1

您的代碼不會編譯或工作... – CodingYoshi

+0

可能是因爲您不能在算術運算中使用字符串,這是第一行中的問題,我沒有在尋找它,因爲它不是原始問題的一部分,現在編輯。 – user45940

+0

你是如何將tb1聲明爲一個String並將其賦值爲int的?更不用說大寫'If'和'Int'和'For' ......我可以看到你的代碼不會僅僅通過查看而編譯。 –

0

您可以添加一個「反」的循環中,你正在做的刪除。

例子:

 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