eMi eMi -4 years ago 123
C# Question

if ListBox Contains, don't add

I've got a Method:

FillListBox();


I call this method from different places.. But sometimes it happens, that things were loaded twice!

Now I'm trying to do something like:

if (listBox.Items[1].ToString() == "hello")
{
DO NOT FILL
}
else
{
FILL
}


THIS DONT WORKS! :(

Fault: InvalidArgument=Value of '1' is not valid for 'index'.
Parameter name: index


And something like that:

if(listBox.Items.Contains("hello"))
{
DONT FILL
}


Dont works too :(

What can I do?

Answer Source

I solved the problem.. I just used myListBox.Items.Clear();

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download