Solved

search list box while typing characters in the text box

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Clone Visual studio 2013 solution 5 24
VB.NET HttpWebRequest 12 57
Close word object 13 38
how to use laptop or pad camera in vb.net windows application 2 54
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…

920 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

13 Experts available now in Live!

Get 1:1 Help Now