我一直在尝试找出如何在列表框中的任何地方收集字符串,我使用Visual Basic 2010,但这更多是一个请求,但是我找到了代码,因此您可以修复找到的代码或告诉我另一个要使用的代码。
我尝试使用ListBoxName.Items.Contains,但是那行不通,我尝试了很多方法,很难一口气说完。
' Split string based on space
Dim textsrtring As String = ListBox.Text
Dim words As String() = textsrtring.Split(New Char() {" "c})
Dim found As Boolean = False
' Use For Each loop over words
Dim word As String
For Each word In words
If ListBox.Items.Contains(word) Then
found = True
Exit For
End If
Next
MessageBox.Show(found)
它们没有错误,出现的消息框不断告诉我错误,当我清楚地输入时没有字符串,也没有错误消息。
答案 0 :(得分:0)
您将需要一个内部循环,以使用String.Contains()查看主列表框条目中是否包含每个单词:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim searchFor As String = TextBox1.Text.Trim
If searchFor.Length > 0 Then
Dim words As String() = searchFor.Split(New Char() {" "c})
Dim found As Boolean = False
Dim foundAt As Integer
' Use For Each loop over words
Dim word As String
For Each word In words
For i As Integer = 0 To ListBox.Items.Count - 1
If ListBox.Items(i).ToString.Contains(word) Then
found = True
foundAt = i
Exit For
End If
Next
If found Then
Exit For
End If
Next
If found Then
ListBox.SelectedIndex = foundAt
Label1.Text = "Search string found."
Else
ListBox.SelectedIndex = -1
Label1.Text = "Search string NOT found."
End If
Else
ListBox.SelectedIndex = -1
Label1.Text = "No search string."
End If
End Sub