vb.net 检查列表框是否包含文本框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19065468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if listbox contains textbox
提问by Jedi
I know I could use .FindStringfor this but for some reason it is not working.
我知道我可以使用.FindString它,但由于某种原因它不起作用。
Basically,if listbox items contains just a PART of textbox text,it does action.
基本上,如果列表框项目只包含文本框文本的一部分,它会执行操作。
Here's the example of not-working code :
这是无效代码的示例:
Dim x As Integer = -1
x = ListBox1.FindString(TextBox1.Text)
If x > -1 Then
'dont add
ListBox2.Items.Add("String found at " & x.ToString)
Else
End If
回答by varocarbas
The FindStringmethod returns the first item which starts with the search string (MSDN). If you want to match the whole item, you would have to use FindStringExact(MSDN). If you want to perform more complex searches, you would have to iterate through all the elements in the ListBox.
该FindString方法返回以搜索字符串 ( MSDN)开头的第一项。如果要匹配整个项目,则必须使用FindStringExact( MSDN)。如果要执行更复杂的搜索,则必须遍历ListBox.
UPDATE: Code delivering the exact functionality expected by the OP.
更新:提供 OP 预期的确切功能的代码。
For i As Integer = 0 To ListBox1.Items.Count - 1
If (ListBox1.Items(i).ToString.Contains(TextBox1.Text)) Then
ListBox2.Items.Add("String found at " & (i + 1).ToString) 'Indexing is zero-based
Exit For
End If
Next

