Log in

View Full Version : ListBox and Textbox in VB.NET 2008!


rallyboy
Apr 25, 2009, 05:08 PM
Hello everyone.

Ok so I have in form1 a listbox1 and a textbox1. In the listbox1 I have several items and I want while I type something in the textbox1 it gives me all the similar in listbox1.

For Example:

I already have in listbox1 the item 'ASKME HELPDESK'. When I type in the textbox1 'ASKME', 'ASKME HELPDESK' will be selected in listbox1. Something like a searcher for the listbox.

Im pretty sure this will be difficult but anyone who helps ill appreciate!

Thanks, rallyboy.

Perito
Apr 26, 2009, 01:43 PM
Double-click on textbox1 to create a handler. At the end, it will say "Handles TextBox1.TextChanged".

Write the handler as follows:

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Dim TestStr As String = TextBox1.Text.ToLower ' test regardless of case.
For I As Integer = 0 To ListBox1.Items.Count - 1
If ListBox1.Items(I).ToString.ToLower.IndexOf(TestStr ) = 0 Then ' also ignore case. Index = 0 means it matches the first of the string.
ListBox1.Text = ListBox1.Items(I).ToString ' Pin the listbox to the found item.
Exit For ' Required if you wish to stop on the first match
End If
Next
End Sub
End Class

Viola. Boy, that was tough! Maybe you should pay me for my services ;):D

ScottGem
Apr 26, 2009, 03:05 PM
Viola. Boy, that was tough! Maybe you should pay me for my services ;):D

There is a way to setup your profile to add a Support this Member link that lets people send you a donation via Paypal. It happens rarely, but it does happen.

But the main reason I'm posting is I wonder how similar lists boxes in VB are to the ones in VBA. The reason I ask is that Access Comboboxes have an AutoExpand property that, when set to Yes, do exactly what the OP wants. As they type into the combo, it will bring up the first matching item in the list.

Perito
Apr 26, 2009, 03:35 PM
.NET listboxes don't have the autoexpand property. Comboboxes are quite different in .NET. They don't have the AutoExpand property any longer. I think it's mostly for flexibility in programming, and for consistency throughout the .NET framework.

rallyboy
Apr 27, 2009, 04:30 AM
Double-click on textbox1 to create a handler. At the end, it will say "Handles TextBox1.TextChanged".

Write the handler as follows:

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Dim TestStr As String = TextBox1.Text.ToLower ' test regardless of case.
For i As Integer = 0 To ListBox1.Items.Count - 1
If ListBox1.Items(i).ToString.ToLower.IndexOf(TestStr ) = 0 Then ' also ignore case. Index = 0 means it matches the first of the string.
ListBox1.Text = ListBox1.Items(i).ToString ' Pin the listbox to the found item.
Exit For ' Required if you wish to stop on the first match
End If
Next
End Sub
End Class

Viola. Boy, that was tough! Maybe you should pay me for my services ;):D

Thanks VERY much ! You helped my loads! Thanks again, appreciated it :)