Hey all,

I have a listbox, textbox, and search button on my form. I am populating my listbox with data from a sql database. I need to be able to seach my listbox and filter it. Here is what I have so far:

Sub RefreshListBox()

        Dim DSDept As New DataSet


        DBADataServer.SelectCommand.CommandText = "SELECT DeptName FROM DSDept ORDER by DeptName"
        DBADataServer.Fill(DSDept)

        For i As Integer = 0 To DSDept.Tables(0).Rows.Count - 1
            lstDept.Items.Add((DSDept.Tables(0).Rows(i).Item(0)))

        Next
    End Sub

That populates my listbox. I've looked online and could not find anything useful. If anybody could lend me a hand id appreciate it :)

i assume you want to search the listbox for the text that is written in the textbox when pressed the button?
if so then you could use: ListBox1.FindString(textbox1.text) or ListBox1.FindStringExact(textbox1.text) if that isnt what you wanted then please give us more details.

basically what I'm looking for is when the user types something into the text box, it will filter out everything and only show what was typed in. For example if i had a listbox that had these three items in it:

soap
cat
dog

And the user typed in dog and hit the search button, only dog will be shown. I hope that makes sense.

uhm for the first part (searching while typing) you could do that with the text.changed event from the textbox. dont know if that goes well together with the "findstring" function of the listbox. if not you can do a loop through the listbox items and hide all that dont contains the current text of the textbox.

second part (on hit the button) you can use the find extstring method of the listbox or doing a loop as i just mention above but this time not with "contains"

I get the logic, but does anyone have an code snippets or examples? sorry for being a pest :)

im sorry but i wont provide any code. if you got the logic then following it and write the code.
time for learning by doing ;) not by asking for code snippets

Okay Ill give it a shot

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.