How can i search using a Text box and Listbox in vb.Net?
give me code, in which i can enter a word in a textbox and a listbox appe开发者_如何学Pythonar with a item that has same string in which i enter in a textbox. Please Help me...
I found the following via Google, which sound like the type of things you want to do:
- Autosearch ListBox in VB.NET (WinForms)
- Search Listboxes as You Type (WinForms or is this VB6?)
- Searching for items in a ListBox (WPF)
Using # 1, here is some code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
List1.Items.Add("Computer")
List1.Items.Add("Screen")
List1.Items.Add("Modem")
List1.Items.Add("Printer")
List1.Items.Add("Scanner")
List1.Items.Add("Sound Blaster")
End Sub
Private Sub Text1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Text1.TextChanged
Dim i As Integer = List1.FindString(Text1.Text)
List1.SelectedIndex = i
If Text1.Text = "" Then
List1.SelectedIndex = -1
End If
End Sub
Think pseudocode, you can do this. Grab the text from the textbox. Set a pointer / counter to the listbox and loop through each item until the end of the list. If the textbox value has the same value as the listboxitem.text then you've found a match exit the for loop.
Add this code to texboxchange
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
ListBox1.SelectedIndex = ListBox1.FindString(TextBox1.Text.Trim)
End Sub
精彩评论