检查项目是否在列表框Visual Basic中

时间:2013-11-14 06:12:48

标签: .net vb.net listbox

dim foo as string = "hello"

检查foo是否在listbox1中?

if listbox1.items.contains(foo) then

不起作用

3 个答案:

答案 0 :(得分:2)

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

我是列表框中找到该项目的索引。

答案 1 :(得分:0)

如果它不起作用,则“hello”不是列表框项目集合中的项目。请记住,“你好”,“你好”,“你好”和“你好”都是不同的字符串。另外.Contains只会比较整个项目,它不会在单个项目中找到子字符串。如果这是您想要的,您将需要一个自定义子例程。

答案 2 :(得分:0)

嗨,您可以尝试一下,

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