Solved

search list box while typing characters in the text box

Posted on 2004-08-28
7
1,956 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video discusses moving either the default database or any database to a new volume.
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now