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

28 thoughts on “Search a Listbox in VB and VB.NET

  1. Callum K

    Code does not work for VB 2008. Try this:

    ‘The ListBox = LstName
    ‘The textbox = txtSearch

    Dim thelist As Integer
    Dim textToSearch As String
    Dim theListText As String

    textToSearch = LCase(txtSearch.Text)

    For thelist = 0 To 200

    theListText = LCase(VB6.GetItemString(Lstname, thelist))

    If theListText = textToSearch Then

    Lstname.Text = textToSearch

    End If

    Next

    ‘***Paste into Textbox Class***

    Hope this helps!

    Reply
  2. Callum K

    Forgot to say, For thelist = 0 To 200

    Change the 200 part to match the number of items in the list or have the program count them.

    Reply
  3. Jason Post author

    Hi, thanks for the comment. This code snippet was actually for Visual Basic 5.0 and Visual Basic 6.0. Not the .NET Languages. 🙂 Thanks for the codes though, I may go ahead and add it just in case. Thanks again 🙂

    Jason

    Reply
  4. Jason Post author

    Hi, for multiple columns in your list you will want to use the Listview Control and Not the regular listbox control. The Listview control is documented pretty well at msdn.microsoft.com if you want to learn about it.

    Jason

    Reply
  5. srijan

    i want to count the listbox items how much are there in number and the number should be diplay how much data (in rows) are there

    Reply
  6. Jason Post author

    Hi, the code below will get the Listbox Item Count.

    ________

    If using VB 5.0/6.0

    MsgBox List1.ListCount

    ________

    If using .NET

    MsgBox Listbox1.Items.Count

    _________

    The 2nd part of your question i’m not exactly sure what it is you want.

    Jason

    Reply
  7. Dhilip

    How to do it for Multiple textboxes.. like i have about 6 textboxes for different keywords like as in..

    if someone wants to search for Title or Author or ISBN.. different text boxes..

    Any help?

    THanks!

    Reply
  8. Dr baheer saeedy

    hello sir,
    i need a little help please
    i want to build a medical dictionary in vb 6, i have a textbox, a llist box, a label box and a rich text box,
    i want to write the word in text box and search it from the list box, when wrting for example “aba” the words starting with ab.. should have already appeared in list box, when the word is found i want it to appear in label box samewize the explanation for that word should appear in rich text box.
    please help me by providing the code.
    sincerely yours
    Dr. baheer

    Reply
  9. Dr baheer saeedy

    hello sir,
    i need a little help please
    i want to build a medical dictionary in vb 6, i have a textbox, a llist box, a label box and a rich text box,
    i want to write the word in text box and search it from the list box, when wrting for example “aba” the words starting with ab.. should have already appeared in list box, when the word is found i want it to appear in label box samewize the explanation for that word should appear in rich text box.
    please help me by providing the code.
    sincerely yours
    Dr. baheer

    Reply
  10. Pierre

    I need help on how to get information from a list box that i setup to read a SQL database that populates the listbox upon start up. Currently right now all i get is {system.data.datarowview}. I would like to get the number that shows up. any help would be nice. Thanks Pierre

    Reply
  11. Maverick

    hello sir,
    i need help on how i add an item or words in listbox from textbox which i input a sentence and put it in listbox by words in seperate lines..
    pls…thx

    for e.g.
    Textbox1.Text = ” i hate you”

    Listbox1

    i
    hate
    you

    how can i come up with this output..
    thx sir..

    Reply
  12. jheng

    please help.. how could i write the code for text box.. i will input ID number and in the label box the lastname, firstname and middle name will appear on it…im using vb6 connect to ADO or MS Access.. juz e-mail me poh..jheng1127@yahoo.com..thnxx..godbless

    Reply
  13. red

    hi!, plz help..
    i just i want to ask, if there’s a codes in listbox computation.. adding a number from the listbox then it will output in the label…

    plzhelp… thank you….

    Reply
  14. Merlene

    Can anyone send me a code of how to search an item in the list view in vb.net.The text to be searched is entered in a textbox.

    Reply
  15. Jason Post author

    This source code should search a listview controls items and work in VB.NET 2005/2008/2010. Just set whatever you named the listview control and set the search text.

    listview.FindItemWithText(“My Text”)

    Give that piece of code a try

    Jason

    Reply
  16. zunni

    is this code the code to use to code a rich text box in vb6 (the original code in the post not the comments)? if not how do u do it??
    thanks!

    Reply
  17. Mustafa

    Hi i am soo greatfull for all is there any simple code about searching in list for example if I added name in list and i want to find it how ? can i joint text box with listbox for find this name ? i need to know that

    Reply
  18. Mhyx

    how to code in a textbox that if you will start to type an integer the choices will automatically appear… I’m using vb 2005

    thankz a lot for the response
    the textbox is txtreceipt

    Reply
  19. Janna

    how to code percentage basing the computation in the datetimepicker
    this is my code but does not work
    If txtdate.Text = “4/28/2011” Then
    txtpenalty.Text = Val(txttotal.Text) * 0.08
    Else
    txtpenalty.Text = Val(txttotal.Text) * 0.08
    End If

    march additional 6%
    april 8% etc…dec 24%..
    thank you for the reply..

    GOD BLESS!!!!!

    Reply
  20. help me!!!

    i need help with putting Lastname, Firstname Middlename in a list box with these categories filled:
    Whole Name: Firstname Middle Lastname
    With Initial: Firstname M. Lastname
    Last Name: Lastname
    First Name: Firstname
    Middle Name: Middle
    Letter Count: 23
    Initials: FML

    Reply
  21. khaos

    Some help please, I have a listbox with some of the items having the same number (3-4 digits in length and starting at the 9 space),some items are individual, but I need way to add the same items to its own array and individual to its own array. so:

    xxxxxxxxx123xxxxx
    xxxxxxxxx123xxxxx
    xxxxxxxxx345xxxxx
    xxxxxxxxx6789xxxx
    xxxxxxxxx6789xxxx
    xxxxxxxxx6789xxxx

    so the first two items would be in its own array
    then 3rd item in its own array
    then the last 3 in its own array

    the listbox may contain 5 items or hundreds, it is unknown.

    Reply
  22. jem

    how to do the code that when you click an item in the listview the possible result will see in the listbox?

    Reply

Leave a Reply