Tag Archives: search listbox control

Search a Listbox in VB and VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (7 votes, average: 3.71 out of 5)
 

This first code snippet is for VB 5.0 and VB 6.0 Only. Not the DotNET languages.

Visual Basic 6.0 (5.0 as well)

'Need 1 button, 1 textbox, 1 listbox</pre>
'Name textbox = txtSearch, Name listbox = lstSearch

Dim theList AsLong

Dim textToSearch AsString

Dim theListText AsString

textToSearch = LCase(txtSearch.Text)

For theList = 0 To lstSearch.ListCount - 1

theListText = LCase(lstSearch.List(theList))

If theListText = textToSearch Then lstSearch.Text = textToSearch

Next

I went ahead and added VB.NET codes per Callum’s comment. This code should work with All .NET languages including Visual Basic 2005 and VB 2008.

Visual Basic.NET 2002/2003, and VB 2005/2008

'
'Added this code for the .NET Languages. Works with VB 2008 as well.
'Thanks to Callum for the code changes.
'

Dim theList As Long 

Dim textToSearch As String 

Dim theListText As String 

textToSearch = LCase(txtSearch.Text)
 
For theList = 0 To lstSearch.Items.Count – 1
 
    theListText = LCase(lstSearch.Items.Item(theList)) 

    If theListText = textToSearch Then lstSearch.Text = textToSearch
 
Next