Click here to Skip to main content
Licence 
First Posted 11 Sep 2006
Views 53,881
Bookmarked 28 times

ListBox Searching

By | 11 Sep 2006 | Article
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

 <FONT size=2><P></FONT><FONT color=#0000ff size=2>Private</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Sub</FONT><FONT size=2> btnSearch_Click(</FONT><FONT color=#0000ff size=2>ByVal</FONT><FONT size=2> sender </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> System.Object, </FONT><FONT color=#0000ff size=2>ByVal</FONT><FONT size=2> e </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> System.EventArgs) _</FONT></P><P><FONT color=#0000ff size=2>  Handles</FONT><FONT size=2> btnSearch.Click</FONT><FONT size=2></P><P></FONT><FONT color=#008000 size=2>      '//removes all items from the second List Box</P></FONT><FONT size=2><P>    ListBox2.Items.Clear()</P><P></FONT><FONT color=#008000 size=2>      '//gets the total number of items in the first ListBox</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>    Dim</FONT><FONT size=2> listLength </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Integer</FONT><FONT size=2> = (ListBox1.Items.Count - 1)</P><P></FONT><FONT color=#008000 size=2>      '//i -> counter through loops : j ->

Now without the comments.

<FONT size=2><P></FONT><FONT color=#0000ff size=2>Private</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Sub</FONT><FONT size=2> btnSearch_Click(</FONT><FONT color=#0000ff size=2>ByVal</FONT><FONT size=2> sender </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> System.Object, </FONT><FONT color=#0000ff size=2>ByVal</FONT><FONT size=2> e </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> System.EventArgs) _</FONT></P><P>  <FONT color=#0000ff size=2>Handles</FONT><FONT size=2> btnSearch.Click</P><P>    ListBox2.Items.Clear()</P><P></FONT><FONT color=#0000ff size=2>    Dim</FONT><FONT size=2> listLength </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Integer</FONT><FONT size=2> = (ListBox1.Items.Count - 1)</P><P></FONT><FONT color=#0000ff size=2>    Dim</FONT><FONT size=2> i, j </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Integer</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>    Dim</FONT><FONT size=2> listString, newString </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>    If</FONT><FONT size=2> radioFull.Checked = </FONT><FONT color=#0000ff size=2>True</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>        For</FONT><FONT size=2> i = 0 </FONT><FONT color=#0000ff size=2>To</FONT><FONT size=2> listLength</P><P>            listString = ListBox1.Items.Item(i)</P><P></FONT><FONT color=#0000ff size=2>            If</FONT><FONT size=2> InStr(listString.ToLower, TextBox1.Text.ToLower) </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P>                ListBox2.Items.Add(listString)</P><P></FONT><FONT color=#0000ff size=2>            End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>If</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>        Next</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>    ElseIf</FONT><FONT size=2> radioIndex.Checked = </FONT><FONT color=#0000ff size=2>True</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>        For</FONT><FONT size=2> i = 0 </FONT><FONT color=#0000ff size=2>To</FONT><FONT size=2> listLength</P><P>            listString = ListBox1.Items.Item(i)</P><P></FONT><FONT color=#0000ff size=2>            For</FONT><FONT size=2> j = 0 </FONT><FONT color=#0000ff size=2>To</FONT><FONT size=2> listString.Length - 1</P><P></FONT><FONT color=#0000ff size=2>                If</FONT><FONT size=2> listString.Substring(j, 1) <> Chr(32) </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P>                    newString += listString.Substring(j, 1)</P><P></FONT><FONT color=#0000ff size=2>                Else</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>                    If</FONT><FONT size=2> InStr(newString, TextBox1.Text) </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P>                        ListBox2.Items.Add(ListBox1.Items.Item(i))</P><P></FONT><FONT color=#0000ff size=2>                    End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>If</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>                Exit</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>For</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>                End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>If</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>            Next</P></FONT><FONT size=2><P>            newString = </FONT><FONT color=#0000ff size=2>Nothing</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>        Next</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>    ElseIf</FONT><FONT size=2> radioText.Checked = </FONT><FONT color=#0000ff size=2>True</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>        Dim</FONT><FONT size=2> spaceCharCounter </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Integer</FONT><FONT size=2> = 0</P><P></FONT><FONT color=#0000ff size=2>        For</FONT><FONT size=2> i = 0 </FONT><FONT color=#0000ff size=2>To</FONT><FONT size=2> listLength</P><P>            listString = ListBox1.Items.Item(i)</P><P></FONT><FONT color=#0000ff size=2>            For</FONT><FONT size=2> j = 0 </FONT><FONT color=#0000ff size=2>To</FONT><FONT size=2> listString.Length - 1</P><P></FONT><FONT color=#0000ff size=2>                If</FONT><FONT size=2> spaceCharCounter >= 2 </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P>                    newString += listString.Substring(j, 1)</P><P></FONT><FONT color=#0000ff size=2>                ElseIf</FONT><FONT size=2> listString.Substring(j, 1) = Chr(32) </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P>                    spaceCharCounter += 1</P><P></FONT><FONT color=#0000ff size=2>                End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>If</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>            Next</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>            If</FONT><FONT size=2> InStr(newString.ToLower, TextBox1.Text.ToLower) </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P>                ListBox2.Items.Add(ListBox1.Items.Item(i))</P><P></FONT><FONT color=#0000ff size=2>            End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>If</P></FONT><FONT size=2><P>            listString = </FONT><FONT color=#0000ff size=2>Nothing</P></FONT><FONT size=2><P>            spaceCharCounter = 0</P><P>            newString = </FONT><FONT color=#0000ff size=2>Nothing</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>        Next</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>    ElseIf</FONT><FONT size=2> radioAdvText.Checked = </FONT><FONT color=#0000ff size=2>True</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>        Dim</FONT><FONT size=2> spaceCharCounter </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Integer</FONT><FONT size=2> = 0</P><P></FONT><FONT color=#0000ff size=2>        For</FONT><FONT size=2> i = 0 </FONT><FONT color=#0000ff size=2>To</FONT><FONT size=2> listLength</P><P>            listString = ListBox1.Items.Item(i)</P><P></FONT><FONT color=#0000ff size=2>            For</FONT><FONT size=2> j = 0 </FONT><FONT color=#0000ff size=2>To</FONT><FONT size=2> listString.Length - 1</P><P></FONT><FONT color=#0000ff size=2>                If</FONT><FONT size=2> spaceCharCounter >= 2 </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P>                    newString += listString.Substring(j, 1)</P><P></FONT><FONT color=#0000ff size=2>                ElseIf</FONT><FONT size=2> listString.Substring(j, 1) = Chr(32) </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P>                    spaceCharCounter += 1</P><P></FONT><FONT color=#0000ff size=2>                End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>If</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>            Next</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>            If</FONT><FONT size=2> InStr(newString, TextBox1.Text) </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P>                ListBox2.Items.Add(ListBox1.Items.Item(i))</P><P></FONT><FONT color=#0000ff size=2>            End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>If</P></FONT><FONT size=2><P>            listString = </FONT><FONT color=#0000ff size=2>Nothing</P></FONT><FONT size=2><P>            spaceCharCounter = 0</P><P>            newString = </FONT><FONT color=#0000ff size=2>Nothing</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>        Next</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>    End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>If</P></FONT><FONT size=2><P>    listString = </FONT><FONT color=#0000ff size=2>Nothing</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Sub</FONT></P><FONT color=#0000ff></FONT>

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!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Yep-ItsMe

Web Developer

United States United States

Member

3+ years Visual Basic.NET programming experience. A little ASP.NET and HTML as well.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questioncan i have exmaple in c# PinmemberManasAddy3:41 7 May '09  
Jokeexcellent PinmemberKuba.cz9:21 6 Oct '08  
QuestionProblem about Listbox properties [modified] PinmemberNewbie_dydy8523:30 15 Oct '06  
GeneralToo much code! Pinmemberrpgmaker4:44 19 Sep '06  
QuestionWhat ever happened to white space? PinmemberBkins8:14 12 Sep '06  
AnswerRe: What ever happened to white space? PinmemberAnthony Queen9:51 12 Sep '06  
AnswerRe: What ever happened to white space? PinmemberYep-ItsMe11:23 12 Sep '06  
GeneralLarge number of items PinmemberItay Sagui22:40 11 Sep '06  
AnswerRe: Large number of items PinmemberYep-ItsMe6:43 12 Sep '06  
GeneralHTML Editor Problems PinmemberYep-ItsMe11:33 11 Sep '06  
AnswerRe: HTML Editor Problems PinmemberAnthony Queen12:03 11 Sep '06  
GeneralRe: HTML Editor Problems PinmemberYep-ItsMe15:06 11 Sep '06  
GeneralVoting... [modified] PinmemberYep-ItsMe11:31 11 Sep '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 11 Sep 2006
Article Copyright 2006 by Yep-ItsMe
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid