שתף באמצעות


How to Search or Filter Listbox items using textbox help me

Question

Friday, February 12, 2016 1:16 PM

 Public Sub Online(ByVal sender As Object, ByVal pr As Presence) Handles x.OnPresence
If MyBase.InvokeRequired = True Then
            MyBase.BeginInvoke(New PresenceHandler(AddressOf Online), New Object() {sender, pr})
        Else
            Dim jid As Jid = New Jid(pr.From.User)
      If pr.Type = PresenceType.available Then
                If Userlist.Items.Contains(jid.ToString) Then
                Else
                    Userlist.Items.Add(jid.ToString)

                End If
            End If
        End If
    End Sub
  Private Sub Searchtext_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Searchtext.TextChanged
        Dim count As Integer = (Userlist.Items.Count - 1)
        Dim words As String
        For a = 0 To count
            words = Userlist.Items.Item(a)
            If InStr(words.ToLower, Searchtext.Text.ToLower) Then
                Userlist.SelectedItem = words
               
            End If
        Next
    End Sub

How to Filter Listbox item using Textbox

like 

Total UserList :

Filter Userlist:

Please Help me

All replies (5)

Saturday, February 13, 2016 1:48 AM ✅Answered

How to Filter Listbox item using Textbox

Your description indicates that you want to select the filtered items, but your code implies that you want to highlight the filtered items.

This example highlights the filtered items.  If you want to select (eg, put the filtered items into a new list) then you can use the SelectedItems collection that is created.

    Private Sub Searchtext_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Searchtext.TextChanged
        UserList.SelectedItems.Clear()
        If Searchtext.Text <> "" Then
            Dim count As Integer = (UserList.Items.Count - 1)
            Dim words As String
            For a = 0 To count
                words = UserList.Items.Item(a)
                If words.ToLower.StartsWith(Searchtext.Text.ToLower) Then
                    UserList.SelectedItems.Add(UserList.Items.Item(a))
                End If
            Next
        End If
    End Sub

Friday, February 12, 2016 1:28 PM

You could put them in an array first

Live as if you were going to die today, learn as if you were going to live forever.


Friday, February 12, 2016 7:17 PM | 1 vote

My suggestion would be to add your items as DataRows to a DataTable and then bind your control to the DataTable. When you wanted to filter the results, you'd set the RowFilter to some query. Take a look at this example:

Private dt As New DataTable()

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    dt.Columns.Add(New DataColumn("Item")
    Userlist.DataSource = dt
    Userlist.DisplayMember = "Item"
End Sub

Public Sub Online(ByVal sender As Object, ByVal pr As Presence) Handles x.OnPresence
    '...
    If Not Userlist.Items.Contains(jid.ToString()) Then
        dt.Rows.Add({jid.ToString()})
    End If
End Sub

Private Sub Searchtext_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Searchtext.TextChanged
    Userlist.DataSource.DefaultView.RowFilter = String.Format("[Item] like '%{0}%'", Searchtext.Text)
End Sub

Friday, February 12, 2016 9:45 PM

My suggestion would be to add your items as DataRows to a DataTable and then bind your control to the DataTable. When you wanted to filter the results, you'd set the RowFilter to some query. Take a look at this example:

Private dt As New DataTable()

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    dt.Columns.Add(New DataColumn("Item")
    Userlist.DataSource = dt
    Userlist.DisplayMember = "Item"
End Sub

Public Sub Online(ByVal sender As Object, ByVal pr As Presence) Handles x.OnPresence
    '...
    If Not Userlist.Items.Contains(jid.ToString()) Then
        dt.Rows.Add({jid.ToString()})
    End If
End Sub

Private Sub Searchtext_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Searchtext.TextChanged
    Userlist.DataSource.DefaultView.RowFilter = String.Format("[Item] like '%{0}%'", Searchtext.Text)
End Sub

Still Not Wrking


Saturday, February 13, 2016 1:50 AM | 2 votes

Still Not Wrking

That is not helpful.

What exactly did you do, what was the result, and what was wrong with that result?