Solved

search list box while typing characters in the text box

Posted on 2004-08-28
7
2,250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

628 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