1
dim foo as string = "hello"

检查 foo 是否在 listbox1 中?

if listbox1.items.contains(foo) then

不工作

4

3 回答 3

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

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

于 2013-11-14T07:36:35.567 回答
0

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

于 2013-11-14T06:43:32.857 回答
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
于 2019-05-15T05:40:27.793 回答