Neel
Neel

Reputation: 21

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 appear with a item that has same string in which i enter in a textbox. Please Help me...

Upvotes: 0

Views: 15115

Answers (3)

monkey from Tanzania
monkey from Tanzania

Reputation: 526

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

Upvotes: 0

Matt
Matt

Reputation: 14531

I found the following via Google, which sound like the type of things you want to do:

  1. Autosearch ListBox in VB.NET (WinForms)
  2. Search Listboxes as You Type (WinForms or is this VB6?)
  3. 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

Upvotes: 2

JonH
JonH

Reputation: 33143

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.

Upvotes: 1

Related Questions