Question Listbox items filtered by textbox onchange

mrtlcstd

Member
Joined
Nov 21, 2013
Messages
7
Programming Experience
1-3
I have a textbox and listbox on a form. The listbox is bound to items from a database.

When the user begins to type in the textbox I'd like the listbox items to be filtered onchange as he types. So according to what is being typed the items listbox will lessen according to what matches what is being typed. *This is not an autocomplete function.*

Can someone please point me to some sample code to be able to achieve this? Thanks!
 
What you want is very easy if your data is stored in a DataTable. If you are populating a DataTable and then binding that to the ListBox then the code is very simple. I would advise using a BindingSource, i.e. binding the DataTable to the BindingSource and the BindingSource to the ListBox. The code then looks something like this:
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    Me.BindingSource1.Filter = String.Format("SomeColumn LIKE '{0}%'", Me.TextBox1.Text)
End Sub
This is not ideal though. This is going to filter the data every time the text changes. Let's say that the user wants to see all items starting with "abc". They will type "a" and the data will be filtered, then they'll type "b" and it will be filtered again and then it will be filtered a third time when they type "c". The first two filters are useless and may actually hinder a fast typist because the app will be busy filtering instead of processing the next key stroke. A better approach is to only filter when the user stops typing. You have to make a judgement about what it means to have stopped typing, but something like half a second since the last keystroke should work fine. You can shorten or lengthen that as you like.

To do this, use a Timer with its Interval set to 500 (milliseconds). When they hit a key you start the Timer and each time they hit another key within the time threshold you restart it. Only when the time expires do you actually filter the data. This will generally provide a better UX, especially for large data sets. E.g.
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    'Stop the Timer if it is already running.
    Me.Timer1.Stop()

    'Start a new time period.
    Me.Timer1.Start()
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    'Time has expired so stop the Timer.
    Me.Timer1.Stop()

    'Filter the data.
    Me.BindingSource1.Filter = String.Format("SomeColumn LIKE '{0}%'", Me.TextBox1.Text)
End Sub
 
It just occurred to me that this might actually be a Web Forms application and I have assumed WinForms. If that is the case then I've wasted my time providing irrelevant advice. That's an example of why you should always post clearly, which includes indicating what type of app it is, and post in the most correct forum. There are forums for both Web Forms and WinForms so, either way, this thread does not belong here in the VB.NET General forum. Please let me know what type of application it is so that I can move the thread to the appropriate forum. If it is ASP.NET then I've wasted my time as the advice I provided will not work. In that case you'd have to use JavaScript code, which is obviously quite different.
 
Thanks for the response Mr. jmcilhinney. But the problem is, my data is a files like .pdf, .docx, etc. and all I need to do is when I search a specific file referring to its type like title, date or maybe a description to it, it must be opened. I have a lot of data containing files, its like 10,000+ files. How am I going to solve this kind of problem? Can you help me with this? Thank you!
 
It's a WebForm based sir, I am using VB.Net so I posted it here. By the way, I appreciated your response. :)

We're all using VB.NET. This whole site is dedicated to VB.NET so every thread is about VB.NET. Not all projects are the same though, so we have forums dedicated to different things. VB.NET General is for questions that don't fall into more specific categories. Your question is specific to ASP.NET so is obviously not just general VB.NET. I will have moved this thread to the Web Forms forum by the time you read this.

You're either going to have to use AJAX to update the contents of the list from the server or else send the whole list to the client and then filter locally using JavaScript. With such a large list, I would suggest that AJAX would be the way to go, so you should start reading up on that to get a grounding in the basics first.
 
We're all using VB.NET. This whole site is dedicated to VB.NET so every thread is about VB.NET. Not all projects are the same though, so we have forums dedicated to different things. VB.NET General is for questions that don't fall into more specific categories. Your question is specific to ASP.NET so is obviously not just general VB.NET. I will have moved this thread to the Web Forms forum by the time you read this.

You're either going to have to use AJAX to update the contents of the list from the server or else send the whole list to the client and then filter locally using JavaScript. With such a large list, I would suggest that AJAX would be the way to go, so you should start reading up on that to get a grounding in the basics first.

Okay sir. Thank you so much!
 
Back
Top