visual-studio-2010 - How to collect a certain string anywhere in an item in a ListBox?
问题描述
I have been trying to find out how to collect a string anywhere in a Listbox, I use Visual Basic 2010 and this is more of an request, but there is code I found so you fix the code I found or tell me me an another code to use.
I have tried using ListBoxName.Items.Contains but that did not work, I tried a lot of methods and it would be hard to say all of then at once.
' 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)
They were no errors, the message box that appeared kept on telling me false and there is no string when I clearly put it in, no error messages.
解决方案
您需要一个内部循环来使用 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
推荐阅读
- node.js - 如何在 Mongodb 中循环数据和验证
- php - PHP为类成员分配一个值并在类中的另一个方法中访问
- java - Java Streams 和 List of List 列表
- jupyter-notebook - 验证已创建的 ipython notebook 输出是否符合预期
- c# - 使用 ajax 和 Razor Pages 的复杂模型绑定
- r - rvest:XPath 和 CSS 不适用于 The Weather Network
- python-3.x - 无法安装 ODBC 以在 Mac OS Mojave 中运行 PYODBC
- java - Selenium WebDriver JAVA 循环或其他方式获取结果
- python - cURL 查询有效,但 python-request 失败
- java - 如何使用 Java 创建 PPTP 连接?