如何为每个项循环listbox.FindString()

时间:2013-04-24 13:51:30

标签: c# search loops listbox

我很难找到一种方法来循环listbox.FindString()每个项目来搜索项目列表框。举个例子:

示例代码:

string myString = "hi";

int index = listBox1.FindString(myString, -1);

if (index != -1) {
    listBox1.SetSelected(index, true);
    MessageBox.Show("Found the item \"" + myString + "\" at index: " + index);
}

1 个答案:

答案 0 :(得分:2)

您可以使用while循环:

int index = ListBox.NoMatches;
while ((index = listBox1.FindString(myString, index)) != ListBox.NoMatches)
{
    MessageBox.Show("Found the item \"" + myString + "\" at index: " + index);
}