开发者

How can i search using a Text box and Listbox in vb.Net?

开发者 https://www.devze.com 2023-01-23 12:57 出处:网络
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.

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:

  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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消