65.9K
CodeProject is changing. Read more.
Home

ListBox Searching

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.14/5 (8 votes)

Sep 11, 2006

viewsIcon

77842

downloadIcon

3102

How to search ListBoxes: Full Search: Index Search: Text Search: Case Sensative Text Search

Sample image

Introduction

This code shows you how to search through a ListBox (ListBox1) and add all Items that match the search query (from TextBox1) to a second ListBox (ListBox2).

Using The Source Code

I have included two sets of the source code, one with comments, and one without.  I did this because the begginers have a better chance of needed comments to walk them through the steps, but they might just be more in the way than anything else for the more advanced programmers.

The Source Code

 

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _

Handles btnSearch.Click

'//removes all items from the second List Box

ListBox2.Items.Clear()

'//gets the total number of items in the first ListBox

Dim listLength As Integer = (ListBox1.Items.Count - 1)

'//i -> counter through loops : j -> counter through chars in string

Dim i, j As Integer

'//listString -> string of item in ListBox1

'//newString -> gets added to one char at a time from listString

Dim listString, newString As String

'//self explanitory, lol

If radioFull.Checked = True Then

'//loop through all items in ListBox1

For i = 0 To listLength

'//one at a time in sequential order

listString = ListBox1.Items.Item(i)

'//searches ListBox1 Item for the text in TextBox1

'//turning all text to lowercase

If InStr(listString.ToLower, TextBox1.Text.ToLower) Then

'//if text is found then add ListBox1 Item to ListBox2

ListBox2.Items.Add(listString)

End If

Next

'//search Item Index

ElseIf radioIndex.Checked = True Then

For i = 0 To listLength

listString = ListBox1.Items.Item(i)

'//loop through listString 1 char at a time

For j = 0 To listString.Length - 1

'//goes through first If until you get to a Space Char

'//if char is NOT Space Char then

If listString.Substring(j, 1) <> Chr(32) Then

'//add char to newString

newString += listString.Substring(j, 1)

Else '//once you get to a Space Char

'//searches ListBox1 Item for the text in TextBox1

If InStr(newString, TextBox1.Text) Then

'//add Item to ListBox2

ListBox2.Items.Add(ListBox1.Items.Item(i))

End If

'//Exit For Statement

Exit For

End If

Next

'//empties newString for the next round!

newString = Nothing

'//time to search next Item in ListBox1

Next

'//search Text, non Case Sensative

ElseIf radioText.Checked = True Then

'//holds number of Space Chars

Dim spaceCharCounter As Integer = 0

For i = 0 To listLength

listString = ListBox1.Items.Item(i)

'//go through Item 1 char at a time

For j = 0 To listString.Length - 1

'//if spaceCharCounter is greater than or equal to 2

If spaceCharCounter >= 2 Then

'//add Char to newString

newString += listString.Substring(j, 1)

'//if char in listString = Space Char

ElseIf listString.Substring(j, 1) = Chr(32) Then

'//add 1 to spaceCharCounter

spaceCharCounter += 1

End If

'//next Char in ListBox1 Item

Next

'//search with all lowercase

If InStr(newString.ToLower, TextBox1.Text.ToLower) Then

'//add Item to ListBox2

ListBox2.Items.Add(ListBox1.Items.Item(i))

End If

'//delete values

listString = Nothing

spaceCharCounter = 0

newString = Nothing

'//search next Item in ListBox1

Next

'//search Text, Case Sensative

ElseIf radioAdvText.Checked = True Then

Dim spaceCharCounter As Integer = 0

For i = 0 To listLength

listString = ListBox1.Items.Item(i)

For j = 0 To listString.Length - 1

If spaceCharCounter >= 2 Then

newString += listString.Substring(j, 1)

ElseIf listString.Substring(j, 1) = Chr(32) Then

spaceCharCounter += 1

End If

Next

'//***The one difference between the last 2 radio buttons

'//***There is NO .ToLower in search function

If InStr(newString, TextBox1.Text) Then

ListBox2.Items.Add(ListBox1.Items.Item(i))

End If

listString = Nothing

spaceCharCounter = 0

newString = Nothing

Next

End If

listString = Nothing

End Sub

 

Now without the comments.

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _

Handles btnSearch.Click

ListBox2.Items.Clear()

Dim listLength As Integer = (ListBox1.Items.Count - 1)

Dim i, j As Integer

Dim listString, newString As String

If radioFull.Checked = True Then

For i = 0 To listLength

listString = ListBox1.Items.Item(i)

If InStr(listString.ToLower, TextBox1.Text.ToLower) Then

ListBox2.Items.Add(listString)

End If

Next

ElseIf radioIndex.Checked = True Then

For i = 0 To listLength

listString = ListBox1.Items.Item(i)

For j = 0 To listString.Length - 1

If listString.Substring(j, 1) <> Chr(32) Then

newString += listString.Substring(j, 1)

Else

If InStr(newString, TextBox1.Text) Then

ListBox2.Items.Add(ListBox1.Items.Item(i))

End If

Exit For

End If

Next

newString = Nothing

Next

ElseIf radioText.Checked = True Then

Dim spaceCharCounter As Integer = 0

For i = 0 To listLength

listString = ListBox1.Items.Item(i)

For j = 0 To listString.Length - 1

If spaceCharCounter >= 2 Then

newString += listString.Substring(j, 1)

ElseIf listString.Substring(j, 1) = Chr(32) Then

spaceCharCounter += 1

End If

Next

If InStr(newString.ToLower, TextBox1.Text.ToLower) Then

ListBox2.Items.Add(ListBox1.Items.Item(i))

End If

listString = Nothing

spaceCharCounter = 0

newString = Nothing

Next

ElseIf radioAdvText.Checked = True Then

Dim spaceCharCounter As Integer = 0

For i = 0 To listLength

listString = ListBox1.Items.Item(i)

For j = 0 To listString.Length - 1

If spaceCharCounter >= 2 Then

newString += listString.Substring(j, 1)

ElseIf listString.Substring(j, 1) = Chr(32) Then

spaceCharCounter += 1

End If

Next

If InStr(newString, TextBox1.Text) Then

ListBox2.Items.Add(ListBox1.Items.Item(i))

End If

listString = Nothing

spaceCharCounter = 0

newString = Nothing

Next

End If

listString = Nothing

End Sub

The End

I would have seperated the different RadioButton.Checked statements for easier reading, but I'm having some problems with the editor.

Have Fun!