vb.net 检查项目是否在列表框中 Visual Basic
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19970811/
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 item is in listbox Visual Basic
提问by BahNahNah
dim foo as string = "hello"
check if foo is in listbox1?
检查 foo 是否在 listbox1 中?
if listbox1.items.contains(foo) then
does not work
不起作用
回答by Codemunkeee
Dim foo As String
foo = "Hello"
For i As Integer = 0 To ListBox1.Items.Count - 1
If ListBox1.Items(i).ToString = foo Then
MsgBox(i)
End If
Next
i is the index on the listbox where the item was found.
i 是找到该项目的列表框上的索引。
回答by tinstaafl
If it's not working then "hello" isn't an item in the listbox items collection. Remember that " hello","hello ","Hello", and "hello" are all different strings. Also .Contains will only compare whole items it won't find a substring within an individual item. You'll need a custom sub routine if that's what you want.
如果它不起作用,则“hello”不是列表框项目集合中的项目。请记住,“hello”、“hello”、“Hello”和“hello”都是不同的字符串。此外 .Contains 只会比较整个项目,它不会在单个项目中找到子字符串。如果这是您想要的,您将需要一个自定义子例程。
回答by Dev I.A
hi you can try it out,
你好,你可以试试
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim found As String = ""
Dim foo As String
foo = "hello"
For i As Integer = 0 To ListBox1.Items.Count - 1
If ListBox1.Items(i).ToString = foo Then
found = (i)
End If
Next
If found = "" Then
MessageBox.Show("not found your word!!")
Else
MessageBox.Show("found hello, word!")
End If
End Sub

