2010-06-26 69 views
0

所以我想刪除列表框中的空項目,如空白,所以我有這個代碼。但編譯器給我一個錯誤我試圖刪除C列表框中的空項目#

for (int i = 0; i < listBox2.Items.Count; i++) 
{ 
    if (listBox2.Items[i].ToString = " "){//ERROR* 
     listBox2.Items.RemoveAt(i); 
    } 
} 

*無法將方法組'ToString'轉換爲非委託類型'bool'。你打算採用這種方法嗎?

回答

8

ToString是一種方法,所以你需要它是ToString(),等號比較是用兩個等號==執行的,而不是一個。一個等號是分配的。這就是說,要迭代你的收藏並刪除它們的索引項,你就需要反過來。你會注意到,當你移除物品時,你的物品數量顯然會下降,所以你的循環不會像你想象的那樣。所以要爲這樣的事情:

int count = listBox2.Items.Count; 
for (int i = count - 1; i >= 0; i--) 
{ 
    if (listBox2.Items[i].ToString() == " ") 
    { 
     listBox2.Items.RemoveAt(i); 
    } 
} 
+0

另外,我要指出,這將只匹配是一個空格,只有一個空間的字符串。要匹配所有的空白字符串,使用'String.IsNullOrWhitespace(listbox2.Items [i] .ToString())' – MiffTheFox 2010-06-26 21:59:03

+1

@Miff,這是一個很好的考慮點。它是一個.NET 4方法,因此如果在3.5或更少的框架中工作,它可能不可用。 – 2010-06-26 22:00:26

+2

在<.NET4的情況下,您仍然可以使用'string.IsNullOrEmpty(foo.Trim())'。我敢肯定,這是問題的作者所打算的,而不是單一的空間。 – 2010-06-26 22:10:51

0

你應該試試這個

> for (int i = 0; i <= listBox2.Items.Count; i++) 
>    { 
>     if (listBox2.Items[i].ToString() == "") //Remove ' ' space 
>      listBox2.Items.RemoveAt(i); 
>     } 
>    } 
+1

代碼仍然會出現錯誤,因爲缺少等號並且不會匹配問題所需的空格。你所做的只會找到空字符串(一旦錯誤被修復)。 – 2010-06-26 21:58:38

+0

沒有錯誤是關於=。應該是==。 – RvdK 2010-06-26 21:59:12

2

嘗試

if (String.IsNullOrWhiteSpace(listBox2.Items[i].ToString())){ 

但是!由於您要移除項目,這意味着枚舉器無法正常工作。如果您的列表框中的項目是

  • 「一」
  • 「」
  • 「B」

那麼你的代碼將:

  • 保持一個。 (索引0)
  • 刪除「」。 (索引1,「b」成爲新索引1)
  • 拋出IndexOutOfRangeException。 (因爲沒有項目2)

相反,嘗試

List<int> itemsToRemove = new List<int>(); // using System.Collections.Generic 
for (int i = 0; i <= listBox2.Items.Count; i++) 
{ 
    if (String.IsNullOrWhiteSpace(listBox2.Items[i].ToString())){ 
     itemsToRemove.Append(i); 
    } 
} 
foreach (int i in itemsToRemove){ 
    listBox2.Items.RemoveAt(i); 
} 
+0

或者,改變原來的'for'循環減少:'for(int i = listBox2.Items.Count - 1; i> = 0; i - )'。這樣就可以了,無需創建列表並運行兩個週期。 – 2010-06-26 22:01:57

+0

String.IsNullOrWhitespace僅在.NET 4.0中可用。 – 2010-06-26 22:04:30

0

看起來你是一個VB-傢伙。 在基於C的語言(如C#)中有(不像在VB中)兩個不同的運算符用於分配內容和檢查內容(我不知道真正的術語)。爲了檢查兩件事是否相等,你使用double =(==)。所以你在if語句中做了什麼,是將「」分配給列表框項目。這顯然不會返回一個布爾值(這是如果需要的話)。在pre-c#語言中,這可能會導致很難找到的錯誤,因爲沒有警告。

在VB中,您可以調用不使用括號的方法。在C#中,它們總是需要的。編譯器會認爲你想把方法的地址存儲在一個委託中,如果你把它們放在外面(就像在VB.Net中使用AddressOf一樣)。當你做出這兩個改變(事實上,像Anthony Pegram所說的那樣向後循環),所有的都會正常工作。

0

僅刪除列表框中的所有空白項目(行)。使用VS 2003框架1.1或VS 2005框架2.0或VS 2008框架3。5,該代碼會爲你工作:

int i = 0; 
while (listBox1.Items.Count - 1 >= i) 
{ 
// convert listbox object to string so we can use Trim() for remove all space(whitespace char before and after the word 
//then check if remain character or there is nothing at all whatever whitspace char or any space 

    if (Convert.ToString(listBox1.Items[i]).Trim() == string.Empty) 
    { 
    //if the line became blank after Trim() apply so the line is empty and condition is true 
    listBox1.Items.RemoveAt(i); 
    //decrement i because we remove line and the following line will take his place and his index number 
    i -= 1; 
    } 
i += 1; 
} 

請記住,如果你點擊在新的空行空格鍵或選項卡創建角色呼叫空格字符,這不是空行明白我的意思,如果我們將下列行添加到列表框

  listBox1.Items.Add(" IN ");//click tab before and after IN 
      listBox1.Items.Add("");  //blank line no whitespace char or any character 
      listBox1.Items.Add(" THE"); //click spacebar twice before THE 
      listBox1.Items.Add(" "); //click tab once 
      listBox1.Items.Add(" NAME "); //click spacebar after and before 
      listBox1.Items.Add(" OF "); //click tab before and spacebar after 
      listBox1.Items.Add("  ");//click tab twice 
      listBox1.Items.Add("ALLAH"); //no space after or before 

唯一的線滿足String.Empty條件不適用Trim()到線二號線,但是當我們使用Trim() 4號線和7號線,他們會像第2行空行沒有空格字符等等第二(已經空行或空行不需要修改),第四,七(在使用修剪後變成空行),它們將從列表框中移除。

結果將是:

IN 
    THE 
    NAME 
    OF 
ALLAH