Hi all....

I'm using visual studio 2005 with vb.net.
I've got a radio button and if you check it it loads data from my database into my lisbox. that works fine. but i want to use it as a little search function so i have a textbox and if i type text into the textbox it has to filter my listbox. is their any way to do that...?

im not that good at english soo sorry for that.

thanks a lot in advance..

Recommended Answers

All 3 Replies

I don't know anything about databases but try this:

If Trim(TextBox1.Text) = "" Then Exit Sub
        Dim lb As ListBox = New ListBox
        Dim s As Integer = 0
        For s = 0 To ListBox1.Items.Count - 1
            If InStr(ListBox1.Items(s).ToString, TextBox1.Text) Then
                lb.Items.Add(ListBox1.Items(s))
            End If
        Next

        ListBox1.Items.Clear()
        For s = 0 To lb.Items.Count - 1
            ListBox1.Items.Add(lb.Items(s).ToString)
        Next
        lb.Dispose()

Thank You...,But if use backspace the listbox will be shown empty..., how to i change it...,

If you start with a form containing lbxWords (a ListBox) and txtFilter (a TextBox) then add the following code

Public Class Form1

    Private WordList() As String
    Private Filtered() As String

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        WordList = Split("abatement abatements abater abaters abates abatic abating abatis abatised abatises abatjour abatjours abaton abator abators abattage abattis abattised abattises abattoir abattoirs abattu abattue abature abaue abave abaxial abaxile abay abayah")

    End Sub

    Private Sub txtFilter_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtFilter.TextChanged

        Filtered = Filter(WordList, txtFilter.Text)
        lbxWords.DataSource = Filtered

    End Sub

End Class

every new character in txtFilter will cause only those words containing that string to be shown in the ListBox. If you want only words starting with the entered filter then use

Private Sub txtFilter_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtFilter.TextChanged

    lbxWords.Items.Clear()

    For Each word As String In WordList
        If word.StartsWith(txtFilter.Text) Then
            lbxWords.Items.Add(word)
        End If
    Next

End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.