我有一个方法:
FillListBox();
Run Code Online (Sandbox Code Playgroud)
我从不同的地方调用这个方法..但有时会发生,事情被加载了两次!
现在我正在尝试做类似的事情:
if (listBox.Items[1].ToString() == "hello")
{
DO NOT FILL
}
else
{
FILL
}
Run Code Online (Sandbox Code Playgroud)
这不行!:(
Fault: InvalidArgument=Value of '1' is not valid for 'index'.
Parameter name: index
Run Code Online (Sandbox Code Playgroud)
这样的事情:
if(listBox.Items.Contains("hello"))
{
DONT FILL
}
Run Code Online (Sandbox Code Playgroud)
不要工作:(
我能做什么?
试试这个
if(ListBox.NoMatches != listBox.FindStringExact("StringToFind"))
{
listBox.Items.Add("StringToAdd");
}
Run Code Online (Sandbox Code Playgroud)
或者只是试试这个
bool found = false;
foreach (var item in listBox.Items)
{
if(item.ToString().Equals("StringToAdd"))
{
found = true;
break;
}
}
if(!found)
listBox.Items.Add("StringToAdd");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14398 次 |
| 最近记录: |