So i want to remove empty items in listbox like whitespace so Far I have this code. But the compiler is giving me an error
for (int i = 0; i < listBox2.Items.Count; i++)
{
if (listBox2.Items[i].ToString = " "){//ERROR*
listBox2.Items.RemoveAt(i);
}
}
*Cannot convert method group ‘ToString’ to non-delegate type ‘bool’. Did you intend to invoke the method?
ToStringis a method, so you need it to beToString(), and equality comparisons are performed with two equals signs==, not one. One equals sign is for assignment.With that said, to iterate over your collection and remove items by their index, you’ll want to go in reverse. You’ll notice that as you remove items, your item count will obviously drop, so your loop will not behave like you think it will. So go for something like this: