I've been thoroughly combing * and other sources for the answers to these problems, and have not been able to find a solution that would work cohesively with the steps I need to accomplish.
我已经彻底地搜索了*和其他来源,以获得这些问题的答案,但是还没有找到一个能够与我需要完成的步骤一致的解决方案。
Things I need to do:
我需要做的事情:
- Create an array from a text file and display in a listbox (this is done and works)
- 从文本文件创建一个数组,并在列表框中显示(完成并工作)
- Have user fill in a text box, click a button, and the array is searched for anything matching the text box's value
- 让用户填写一个文本框,单击一个按钮,然后数组被搜索到任何与文本框的值匹配的东西?
- Have the results of the search displayed in a separate listbox
- 搜索结果是否显示在单独的列表框中
Here's what I've got so far, and it's fairly hacked together, so if there's anything that can be improved, naturally, I'd be all for that. `
这是我到目前为止所得到的,而且它是相当容易被破解的,所以如果有任何可以改进的东西,自然地,我将会完全支持它。”
Public Class Form1
Dim lblName As Object
Public colleges As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim colleges() As String = IO.File.ReadAllLines("Colleges.txt")
ListBoxCollege.Items.AddRange(colleges)
End Sub
Private Sub btnSearchGo_Click(sender As Object, e As EventArgs) Handles btnSearchGo.Click
Dim n As Integer, college As String
college = txtCollegeSearchUserInput.Text
n = Array.IndexOf(colleges(), college)
If n <> 1 Then
[[Needs to output into new listbox, preferably here]]
End If
End Sub
If there's anything else needed from VB, I can provide if necessary!
如果VB还有什么需要的话,我可以提供。
1 个解决方案
#1
0
In your case you can do something like this
你可以这样做
For i As Integer = 0 To ListBoxCollege.Items.Count -1
If ListBoxCollege.Items(i).ToString().IndexOf(college, StringComparison.OrdinalIgnoreCase) > -1 Then
findList.Items.Add(ListBoxCollege.Items(i))
End If
Next
The difference here - you calling IndexOf
on array and I call it for each item in list. Therefore I return all matches, while you only the first one
这里的区别是——你调用数组上的IndexOf,我为列表中的每个条目调用它。所以我把所有的火柴都退回来了,而你只是第一个
This is little bit limited in search criteria. You could use regex as well for wild cards etc. Or you store your data (colleges) in System.Data.DataTable
, and you would be able to run Sql Select
queries on it almost like in database.
这在搜索条件上有一定的局限性。您可以将regex用于通配符等,也可以将数据(大学)存储在System.Data中。DataTable,您就可以在它上运行Sql Select查询,就像在数据库中一样。
#1
0
In your case you can do something like this
你可以这样做
For i As Integer = 0 To ListBoxCollege.Items.Count -1
If ListBoxCollege.Items(i).ToString().IndexOf(college, StringComparison.OrdinalIgnoreCase) > -1 Then
findList.Items.Add(ListBoxCollege.Items(i))
End If
Next
The difference here - you calling IndexOf
on array and I call it for each item in list. Therefore I return all matches, while you only the first one
这里的区别是——你调用数组上的IndexOf,我为列表中的每个条目调用它。所以我把所有的火柴都退回来了,而你只是第一个
This is little bit limited in search criteria. You could use regex as well for wild cards etc. Or you store your data (colleges) in System.Data.DataTable
, and you would be able to run Sql Select
queries on it almost like in database.
这在搜索条件上有一定的局限性。您可以将regex用于通配符等,也可以将数据(大学)存储在System.Data中。DataTable,您就可以在它上运行Sql Select查询,就像在数据库中一样。