Search or filter a listbox - Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

For this project I use:

Listbox1
Button1
Textbox1
Label1

Place and rename the controls as follows:
filter listbox in visual basic .net
Each time a user enter a word and click on the search button, if a match is found, item that contains the match will be selected.


Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
     Handles MyBase.Load
        With ListBox1.Items
            .Add("My name is Jimmy")
            .Add("I am from California")
            .Add("I go to UCLA")
            .Add("I like porgramming in VB 2008")
            .Add("I love Visual Basic language")
            .Add("This tutorial shows you how to search a listbox ")
        End With
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
     Handles Button1.Click
        Dim count As Integer = (ListBox1.Items.Count - 1)
        Dim words As String
        For a = 0 To count
            words = ListBox1.Items.Item(a)
            If InStr(words.ToLower, TextBox1.Text.ToLower) Then
                ListBox1.SelectedItem = words
                'or display in msgbox Like
                'msgbox(words)
            End If
        Next
    End Sub
End Class


Share This

Home | About | Contact | Privacy Policy

Copyright visual-basic-tutorials.com 2017 - All Rights Reserved.