Results 1 to 12 of 12

Thread: How to search for items in a combobox?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2014
    Posts
    20

    How to search for items in a combobox?

    I have a combobox with 100+ items. All the items are listed in order of their id and their id is before their name. Some items have the same words but other words aswell. I have created a textbox and a button to search the combobox but I do not know how to code it.

    What I want it to do is search all the items in the combobox until it has been through all the items and then display all the results in a msgbox or something similar. Multiple items have to be hown in the msgbox.

    I could, if it would be easier put all the items in the combobox into a text file in resources so the search would search that?

  2. #2
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: How to search for items in a combobox?

    It makes no sense to put the combobox items into a text file, especially since you can access which items are in the combobox immediately, and add or remove items. Create a StringBuilder and then loop through the items. My example below assumes textbox1.text contains the string you are looking for.

    Code:
            Dim builder As New System.Text.StringBuilder
    
            For Each comboItem In ComboBox1.Items
                If comboItem = TextBox1.Text Then
                    builder.AppendLine(comboItem)
                End If
            Next comboItem
    
            MsgBox(builder.ToString)
    http://www.dotnetperls.com/stringbuilder-vbnet

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2014
    Posts
    20

    Re: How to search for items in a combobox?

    Im sorry I am new to programming and this is my first time using visual basic.

    I dont understand how to use your example. I have checked out the website and dont really understand it. Is there any other ways of doing this?

  4. #4
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: How to search for items in a combobox?

    Quote Originally Posted by matthewmcconnell1997 View Post
    Im sorry I am new to programming and this is my first time using visual basic.

    I dont understand how to use your example. I have checked out the website and dont really understand it. Is there any other ways of doing this?
    What don't you understand? I will comment the code to explain what its doing.

    Code:
            Dim builder As New System.Text.StringBuilder 'create a new stringbuilder class which will hold the values we find
    
            For Each comboItem In ComboBox1.Items 'for each entry in the combobox, replace 'combobox1' with you combobox name
                If comboItem = TextBox1.Text Then 'if the current combobox item matches the entry in 'textbox1', replace 'textbox1' with your textbox name
                    builder.AppendLine(comboItem) 'add the item to the stringbuilder holding the values
                End If 'exit if statement
            Next comboItem 'go to next combobox item
    
            MsgBox(builder.ToString) 'show all values found

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2014
    Posts
    20

    Re: How to search for items in a combobox?

    I only have a small idea to what strings are from what I read on the other page. When writing this code, where does it go? Undter the button? I do the same thing for every item in the combobox dont I? And could you just write a little bit for me with a few random names so I can see how it should look.

    Thankyou. Your help is greatly appreciated.

  6. #6
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: How to search for items in a combobox?

    The code will go under the button click event, so when you press the button, the event will occur which will run the code. Yes, the code will check every item in the combobox equally. The names are irrelevent, the names can be anything, they just need to match up to your controls and make sense to you at least when writing your logic.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jan 2014
    Posts
    20

    Re: How to search for items in a combobox?

    So I just have to copy and paste the code? Only once I do it? If so it didn,t work. The an empty msgbox just appeared. Could you please go through each line of the for me and explain exactly what everything does because I still dont understand.

    Thankyou so much.

  8. #8
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: How to search for items in a combobox?

    Its a copy and paste job to an extent, you have to match your variable names in the designer to the code being written. I went line by line in the code above.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jan 2014
    Posts
    20

    Re: How to search for items in a combobox?

    So every item in the combobox i have to have a seperate peice of code for? If so where do I insert the item name at? Or is there nothing else I have to do?

  10. #10
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: How to search for items in a combobox?

    Quote Originally Posted by matthewmcconnell1997 View Post
    So every item in the combobox i have to have a seperate peice of code for? If so where do I insert the item name at? Or is there nothing else I have to do?
    You really need to read up on for each loops. You use the same piece of code for each item in the combobox. Each item is treated equally and you qualify what you are searching for by giving it an if..then statement.

    Code:
     For Each comboItem In ComboBox1.Items
                If comboItem ="THE VALUE YOU ARE SEARCHING HERE" Then 'change the string to be whatever you are searching for
                    builder.AppendLine(comboItem)
                End If
            Next comboItem
    Speak that code out logically, its no different than saying like: "For each day in the week, if the sky is blue then yell 'LETS GO GOLFING'." You are testing a condition for each day in the week, and then doing something if the given situation matches the condition. The code above says "For each combobox item in all of my combobox items, if the value of the current combobox item equals the string i am looking for, then add that combobox item value to a list, then move to the next item.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jan 2014
    Posts
    20

    Re: How to search for items in a combobox?

    Okay well I have put in the code and tried it but it isnt working. All that happens is an empty msgbox.

  12. #12
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: How to search for items in a combobox?

    Quote Originally Posted by matthewmcconnell1997 View Post
    Okay well I have put in the code and tried it but it isnt working. All that happens is an empty msgbox.
    Are you sure that the value you are searching for is in the list of combobox items and that you are iterating through the correct combobox? If the stringbuilder gave you a blank message that means that your string didnt match any of the items in the combobox list.

    Do the following and see which values you get:

    Code:
    For Each comboItem In ComboBox1.Items
    messagebox.show(comboItem)
            Next comboItem
    Do you see all your applicable values being output into a message box? Those are the values that you can pull as you loop through your combobox items. Perhaps I am speaking french, maybe someone else can put this all in better words than myself.
    Last edited by jayinthe813; Jan 14th, 2014 at 11:55 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width