【问题标题】:How to loop listbox.FindString() for each item如何为每个项目循环 listbox.FindString()
【发布时间】:2013-04-24 13:51:29
【问题描述】:

我一直在试图找到一种方法来循环 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);
}

【问题讨论】:

  • 发生了两件事之一:我误解了您的问题(很可能),或者您误解了 FindString 的工作原理。它已经循环遍历列表框的每个项目并搜索您要查找的内容。它不需要循环工作。您是否尝试设置一个循环来遍历多个列表框?
  • 抱歉,我应该更具体一些。我希望它在找到第一个结果后继续关注,而不是停止。

标签: c# search loops listbox


【解决方案1】:

您可以使用while 循环:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-14
    • 1970-01-01
    • 2020-10-30
    • 2020-10-31
    • 1970-01-01
    相关资源
    最近更新 更多