Results 1 to 7 of 7

Thread: [RESOLVED] How to get ALL the Values from a Combo Box.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2010
    Posts
    81

    Resolved [RESOLVED] How to get ALL the Values from a Combo Box.

    I am familiar with getting a selected value from a combo box but what about if you wanted to get all the values from a combo box then store those values in an array or list.

    The combo box would be data bound to a source and have an ID as the value member. So How would I get all the values from the combo box.

    Thanks.
    Last edited by killbill; Jan 13th, 2011 at 10:33 AM.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: How to get ALL the Values from a Combo Box.

    The .Items property gives you access to all of the items, but exactly what type you see there may be not what you are expecting. I would expect that, from your description, the .Items would be a datarowview, though .Items(x) may return an object. If that is the case, you would have to get the item from the datarowview, because you are getting a whole row rather than just the display item.
    My usual boring signature: Nothing

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: How to get ALL the Values from a Combo Box.

    here's how:

    vb Code:
    1. Dim ids = (From item As DataRowView In ComboBox1.Items.Cast(Of DataRowView)() Select item.Item("ID")).ToArray

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Sep 2010
    Posts
    81

    Re: How to get ALL the Values from a Combo Box.

    Ok thanks what is "ids" is that the array?
    Which array does the code fill?

    Can you explain the code for me please.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: How to get ALL the Values from a Combo Box.

    ids, in that case, will be an array because that is what the code returns. By not specifying a type, the compiler will determine which type is being returned and the variable will be that type. If you paste that code into your project you can highlight ids and it will tell you which type it is.

    What that code does is take all the items in the combobox, which are type Object, and cast them to datarowviews. The code then takes the ID item of those datarowviews and packages them all into an array, which is returned.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Sep 2010
    Posts
    81

    Re: How to get ALL the Values from a Combo Box.

    Ok so how would I manipulate the array. So I could get each value 1 at a time.

    For example,
    Code:
    For Each Value In ids
    ID = ids(Value)
    ' Do something with this data
    ID = 0 ' Reset the value for loop
    Next Value
    
    OR
    
    For index = 0 To ids.Length - 1
                ID = ids(index)
    ' Do something...
            Next
    Is this right? Would this save the data in the array from 0 to last or would I need to count how many items in the array and do a different statement. Thanks.
    Last edited by killbill; Jan 13th, 2011 at 10:30 AM.

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: How to get ALL the Values from a Combo Box.

    yes. it's just a standard array + you'd use it like any array

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