Solved

search list box while typing characters in the text box

Posted on 2004-08-28
7
2,052 Views
Last Modified: 2013-12-03
I use VB.net

I have a Form with a text box and below it a lits box
I want to be able to narrow down the items in the list box as I type characters in the text box.
This si similar to  we see in windows help file
Can I buy a 3rd party control ( or does Microsoft provide this control in VB.net) or can I get a sample code some where on the web to achive this functionality?
or can you refer me to any web page that explains how to acomplish this.
Appreciate help
0
Comment
Question by:KumaraPrathipati
  • 3
7 Comments
 
LVL 3

Accepted Solution

by:
imu79 earned 50 total points
ID: 11922366
Well..a simple way would be to just use the combobox. Set its style property to simple which will give you a textbox and a listbox beneath it. When you start typing the textbox, the listbox will scroll to the item that matches the typed text just as in the help files. However, there is one small problem: the matched item does not get selected as is done in the help file. I'm not sure how big of a problem that would be in your case.

hope this helps..
Imran.
0
 
LVL 3

Assisted Solution

by:GrumbleBot
GrumbleBot earned 50 total points
ID: 11923136
You could try something like this. This will search through the listbox as the text is changed in the text box. If the textbox text is equal to a substring of the listbox item then it will select it. So as you type more characters it will keep updating the selected item in the listbox.

If you want to actuall remove items from the list except those that match the current text then that will be a little different.

    Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Dim i As Integer
        Dim s As String

        For i = 0 To ListBox1.Items.Count - 1
            s = ListBox1.Items(i)
            If String.Compare(s.Substring(0, textbox1.TextLength), TextBox1.Text) = 0 Then
                ListBox1.SelectedIndex = i
                TextBox1.Focus()
                Return
            End If

        Next
    End Sub


hope this helps.
Good luck
0
 
LVL 8

Assisted Solution

by:wguerram
wguerram earned 50 total points
ID: 11934114
This will give you the result you are looking for:

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Dim i As Integer

        With ListBox1
            i = .FindString(TextBox1.Text)
            If i >= 0 Then
                .SelectedItem = .Items(i)
            End If
        End With
    End Sub
0
 
LVL 8

Expert Comment

by:wguerram
ID: 11934127
instead of

.SelectedItem = .Items(i)

you can also use

.SelectedIndex = i
0
 
LVL 8

Expert Comment

by:wguerram
ID: 11934161
Also if do not want to keep the previous item selected when it is not found you have to add this

            If i >= 0 Then
                .SelectedIndex = i
            Else
                .SelectedIndex = -1
            End If
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question