Search code examples
c#listbox

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?


Solution

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