我的目标是开发使用多个文本框的搜索。我有五列(ArticleNo、Description、PartNum、Manufacturer 和 Cost),每列都有一个文本框。
我使用以下方法跟踪原始列表项:
Private originalListItems As New List(Of ListViewItem)
这充满了所有项目(超过 6000 个)。
然后,我将根据创建的五个文本框(tbSearchArticleNo、tbSearchDescription、tbSearchPartNum ... 等)发生五个“文本更改”事件
Private Sub tbSearchArticleNo_TextChanged(sender As Object, e As System.EventArgs) Handles tbSearchArticleNo.TextChanged
If tbSearchDesc.Text <> "" Or tbPartNum.Text <> "" Or tbManufacturer.Text <> "" Or tbCost.Text <> "" Then
SearchCurrentList(lwArticles, tbSearchArticleNo.Text, 0, False)
Else
SearchListView(lwArticles, tbSearchArticleNo.Text, 0, False)
End If
End Sub
这是我的方法 SearchCurrentList:
Private Sub SearchCurrentList(ByVal listview As ListView, ByVal search As String, ByVal colIndex As Integer, ByVal upperCase As Boolean)
If upperCase Then
search = search.ToUpper()
End If
listview.BeginUpdate()
'Clear listview
lwArticles.Items.Clear()
'Other textbox has information in it, concatenate both results
For Each item In currentListItems
Dim itemToUpper = item.SubItems.Item(colIndex).Text
If upperCase Then
itemToUpper = item.SubItems.Item(colIndex).Text.ToUpper()
End If
If itemToUpper.Contains(search) Then
lwArticles.Items.Add(item)
End If
Next
'Reupdate the current list of items
currentListItems.Clear()
For Each item In lwArticles.Items
currentListItems.Add(item)
Next
listview.EndUpdate()
End Sub
这是我的方法 SearchListView:
Private Sub SearchListView(ByVal listview As ListView, ByVal search As String, ByVal colIndex As Integer, ByVal upperCase As Boolean)
'Upper case parameter determines if you're searching a string, if so, it is better to compare everything by uppercase
If upperCase Then
search = search.ToUpper()
End If
listview.BeginUpdate()
If search.Trim().Length = 0 Then
'Clear listview
listview.Items.Clear()
'Clear currentListItems
currentListItems.Clear()
'If nothing is in the textbox make all items appear
For Each item In originalListItems
listview.Items.Add(item)
Next
Else
'Clear listview
listview.Items.Clear()
'Clear currentListItems
currentListItems.Clear()
'Go through each item in the original list and only add the ones which contain the search text
For Each item In originalListItems
Dim currItem = item.SubItems.Item(colIndex).Text
If upperCase Then
currItem = currItem.ToUpper()
End If
If currItem.Contains(search) Then
currentListItems.Add(item)
listview.Items.Add(item)
End If
Next
End If
listview.EndUpdate()
End Sub
这是我的搜索示例:
tbSearchArticleNo.Text = "33"
这将匹配字符串中包含“33”的每个 articleNo。现在我想添加另一个过滤器:
tbSearchDescription.Text = "混合器"
这应该与文章编号中包含 33 以及描述中的“混合器”的所有内容相匹配。等等等等第四个。
实际的过滤器工作正常 - 我唯一的问题是每当我删除某些东西时,例如“Mixer”(虽然文章编号中仍有“33”)它不会返回包含“33”的文章编号的结果......相反它不会改变我的搜索结果。可能有更好的方法来搜索这个?