if ListBox Contains, don't add

2019-07-13 02:52发布

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?

标签: c# listbox
7条回答
男人必须洒脱
2楼-- · 2019-07-13 03:33
String newValue = "hello";
if (listBox1.Items.Cast<Object>().Any(x => x.ToString() == newValue))
{
    return;
}
查看更多
登录 后发表回答