Searching Words In a ListBox in VB.Net

Today, I’m going to teach how to create a simple searching program in a listbox. It enables the user to search the nearest word and highlight the area where the item or word has been search.

So, Open your Visual Basic 2010 or 2013, and create a New Project.


Step 1:

Choose Visual Basic then Windows, Name your project as ‘Listbox’ and Click OK.

Add 1 Label, 1, TextBox, 1 Button and 1 ListBox.



In the Name Properties of Button and ListBox put the Name: btnSearch for Button and lstItem for ListBox. In the Text Property of Label and Button put the Search for Label and Button.

Step 2:
Copy and Paste the code



Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With lstItem.Items
            .Add("Hello World")
            .Add("I am from Philippines")
            .Add("I go to school")
            .Add("I like programming using Visual Basic")
            .Add("This tutorial shows you how to search a listbox")
            .Add("Visit my blog: code-devs.blogspot.com")
        End With
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Dim count As Integer = (lstItem.Items.Count - 1)
        Dim words As String
        For a = 0 To count
            words = lstItem.Items.Item(a)
            If InStr(words.ToLower, TextBox1.Text.ToLower) Then
                lstItem.SelectedItem = words

            End If
        Next
    End Sub
End Class

Final Output:

Searching Words In a ListBox in VB.Net Searching Words In a ListBox in VB.Net Reviewed by code-dev on 10:08 PM Rating: 5

No comments:

Powered by Blogger.