vb2008使用文本框搜索列表框

时间:2013-02-20 21:02:46

标签: vb.net textbox listbox

嗨我有一个表单,可以使用文本框找到或者应该说明列表框中的项目。我有一个用于搜索的文本框和一个填充了数据库中项目的列表框。现在,说列表框项目包括苹果,香蕉,浆果,腰果,柠檬,芒果,花生。如果我在文本框中输入'b',列表框只显示香蕉和浆果..如果我输入'ba',那么listbox只显示香蕉,但如果我输入'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

2 个答案:

答案 0 :(得分: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方法更改为从数据库加载而不是硬编码列表。

答案 1 :(得分:0)

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

你可能想要看看Foreach和List.FindAll。