Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB2008 How to search Listbox for match to Textbox.Text 1

Status
Not open for further replies.

GPerk

Programmer
Jul 6, 2002
161
US
I have a listbox that contains many names in the form Lastname, Firstname Middlename.
The list is sorted.
I want to be able to type a part of a name in a textbox and the program will select the first name in the listbox that matches.

For example, if the listbox contains:
Baker, Larry T.
Jameson, Bart M.
Jones, George Lee
Jones, Noel

Then if "J" is typed in the textbox, the "Jameson" entry would be selected.
But if "Jo" is typed, then the first "Jones" is selected (or both).
If "Jones, N" is typed, "Jones, Noel" is selected.

Is there some way I can avoid coding a search routine for this?
 
Try this, it's not really much code:
Code:
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Me.ListBox1.SelectedItems.Clear()
        If Not Me.TextBox1.Text.Trim = "" Then
            For i As Integer = 0 To Me.ListBox1.Items.Count - 1
                If Me.ListBox1.Items(i).ToString.ToUpper.StartsWith(Me.TextBox1.Text.Trim.ToUpper) Then
                    Me.ListBox1.SelectedItems.Add(Me.ListBox1.Items(i))
                End If
            Next
        End If
    End Sub
 
Thanks, RiverGuy.
Since the Listbox will contain as much as 150,000 items, I was afraid the response would be too slow if I iterated thru the entire list.

But I tried your method and it gives almost instant response, even for the Z's.

Wouldn't it be nice if there were a solution like this:

iMatch = Listbox.Items.Match (Textbox.Text)
 
Just a thought,
Why not use a ComboBox with
"AutocompleteMode" = SuggestAppend
"AutocompleteSource" = ListItems



"Don't worry about the world coming to an end today.
It’s already tomorrow in Australia." - Charles Schulz
 
In C#...

ListBox1.ClearSelected();
if (!(txtAvailable.Text.Trim() == ""))
{
ListBox1.SelectedIndex = ListBox1.FindString(TextBox1.Text);
}
 
In C#...sorry had a type-o in the if (just in case)

ListBox1.ClearSelected();
if (!(TextBox1.Text.Trim() == ""))
{
ListBox1.SelectedIndex = ListBox1.FindString(TextBox1.Text);
}
 

I would just throw a little Exit For to RiverGuy's code when name is found. No need to keep looking if you already found it. :)
Code:
Me.ListBox1.SelectedItems.Clear()
If Not Me.TextBox1.Text.Trim = "" Then
    For i As Integer = 0 To Me.ListBox1.Items.Count - 1
        If Me.ListBox1.Items(i).ToString.ToUpper.StartsWith(Me.TextBox1.Text.Trim.ToUpper) Then
            Me.ListBox1.SelectedItems.Add(Me.ListBox1.Items(i))
            [blue]Exit For[/blue]
        End If
    Next
End If

Have fun.

---- Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top