0

嗨,我有一个表单,可以使用文本框查找或过滤列表框中的项目。我有一个用于搜索的文本框和一个填充了数据库项目的列表框。现在,假设列表框项目包括苹果、香蕉、浆果、腰果、柠檬、芒果、花生。如果我在文本框中输入“b”,列表框只显示香蕉和浆果。如果我输入“ba”,那么列表框只显示香蕉,但如果我输入“be”,那么它会显示浆果等等。我已经得到了这个工作(在 txtSearch 事件中标记为注释的代码)。我的问题是,当用户按下退格键时,如何将列表框中的项目带回来?因为,假设我现在列表框中有香蕉和浆果,当我删除我在文本框中输入的文本时,它应该再次列出所有项目,这样如果我想搜索另一个项目,它将再次被过滤。

代码更新

Public Class Glossary

Private Sub Glossary_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Call List()
    Refreshlist()
End Sub
Private Sub List()
    Dim myCmd As New MySqlCommand
    Dim myReader As MySqlDataReader
    Dim myAdptr As New MySqlDataAdapter
    Dim myDataTable As New DataTable
    Call Connect()
    With Me
        STRSQL = "Select word from glossary"
        Try
            myCmd.Connection = myConn
            myCmd.CommandText = STRSQL
            myReader = myCmd.ExecuteReader
            If (myReader.Read()) Then
                myReader.Close()
                myAdptr.SelectCommand = myCmd
                myAdptr.Fill(myDataTable)
                lstword.DisplayMember = "word"
                lstword.ValueMember = "word"
                If myDataTable.Rows.Count > 0 Then
                    For i As Integer = 0 To myDataTable.Rows.Count - 1
                        lstword.Items.Add(myDataTable.Rows(i)("word"))
                    Next
                End If
            End If
            'lstword.Items.Clear()
            'lstword.Items.AddRange(word.Where(Function(word) word.ToString().Contains(txtSearch.Text)).ToArray())
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        myReader = Nothing
        myCmd = Nothing
        myConn.Close()
        Call Disconnect()
    End With
End Sub

Dim word As List(Of Object)
Private Sub Refreshlist()
    lstword.Items.Clear()
    lstword.Items.AddRange(word.Where(Function(word) word.ToString().Contains(txtSearch.Text)).ToArray())
End Sub
Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
    lstword.Items.Clear()
    lstword.Items.AddRange(word.Where(Function(word) word.ToString().Contains(txtSearch.Text)).ToArray())
    Refreshlist()
    'Call List()
    'lstword.BeginUpdate()

    'Try
    '    ' keep track of the "non-searched items" '
    '    Dim word As New List(Of Object)

    '    lstword.SelectedIndices.Clear()
    '    If txtSearch.Text.Length > 0 Then
    '        For index As Integer = 0 To lstword.Items.Count - 1
    '            Dim item As String = lstword.Items(index).ToString()

    '            If item.IndexOf(txtSearch.Text, StringComparison.CurrentCultureIgnoreCase) >= 0 Then
    '                lstword.SelectedIndices.Add(index)
    '            Else
    '                ' this item was not searched for; we will remove it '
    '                word.Add(index)
    '            End If
    '        Next

    '        ' go backwards to avoid problems with indices being shifted '
    '        For i As Integer = word.Count - 1 To 0 Step -1
    '            Dim indexToRemove As Integer = word(i)
    '            lstword.Items.RemoveAt(indexToRemove)
    '        Next
    '    End If
    'Finally
    '    lstword.EndUpdate()
    'End Try

End Sub
End Class
4

2 回答 2

1

第一步是将项目存储在屏幕外内存中。例如:

Dim words As List(Of Object)

然后,当您刷新列表框时,仅使用该内存列表中与当前条件匹配的项目填充它:

lstword.Items.Clear()
lstword.Items.AddRange(
    words.FindAll(
        Function(word) Return word.ToString().Contains(txtSearch.Text)
        ).ToArray()
    )

或者,使用 LINQ:

lstword.Items.Clear()
lstword.Items.AddRange(
    words.Where(
        Function(word) word.ToString().Contains(txtSearch.Text)
        ).ToArray()
    )

更新

由于您似乎无法使其正常工作,并且很难在没有实际看到它的情况下说出您的代码有什么问题,这里有一个完整的工作示例:

Public Class Form1
    Dim words As New List(Of Object)(New String() {"apple", "banana", "berry", "cashew", "lemon", "mango", "peanut"})

    Private Sub RefreshList()
        lstword.Items.Clear()
        lstword.Items.AddRange(
            words.Where(
                Function(word) word.ToString().Contains(txtSearch.Text)
                ).ToArray()
            )
    End Sub

    Private Sub txtSearch_TextChanged(sender As Object, e As EventArgs) Handles txtSearch.TextChanged
        RefreshList()
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        RefreshList()
    End Sub
End Class

更新 2

我尝试将您的代码与我推荐的建议一起使用,并且效果很好。这是对我有用的代码。试试看,如果它不适合你,请告诉我:

Public Class Glossary
    Private Sub Glossary_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        List()
        Refreshlist()
    End Sub

    Private Sub List()
        word.AddRange(New String() {"apple", "banana", "berry", "cashew", "lemon", "mango", "peanut"})
    End Sub

    Private word As New List(Of Object)()

    Private Sub Refreshlist()
        lstword.Items.Clear()
        lstword.Items.AddRange(word.Where(Function(word) word.ToString().Contains(txtSearch.Text)).ToArray())
    End Sub

    Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        Refreshlist()
    End Sub
End Class

如果可行,那么您需要做的就是更改List从数据库加载的方法,而不是硬编码列表。

于 2013-02-20T21:21:17.457 回答
0

从数据库中获取您的初始列表,将其弹出到 List 类中,例如 List。使用您的搜索文本选择所有匹配的项目并将它们放在列表框中。您只需将它们全部放入空白搜索文本。

您可能还想看看 Foreach 和 List.FindAll。

于 2013-02-20T21:18:04.620 回答