Results 1 to 10 of 10

Thread: Setting & getting Combobox value member

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Setting & getting Combobox value member

    I'm trying to populate a combobox in code with values as follows:

    Code:
            Me.ComboBox1.Items.Add(New KeyValuePair(Of String, Integer)("Quick", 10000))
            Me.ComboBox1.Items.Add(New KeyValuePair(Of String, Integer)("Short", 25000))
            Me.ComboBox1.Items.Add(New KeyValuePair(Of String, Integer)("Medium", 50000))
            Me.ComboBox1.Items.Add(New KeyValuePair(Of String, Integer)("Long", 100000))
            Me.ComboBox1.DisplayMember = "Key"
            Me.ComboBox1.ValueMember = "Value"

    And then to get the value I'm doing this in a button click event:

    Code:
     Dim tmp As Integer = CInt(Me.ComboBox1.SelectedValue)
    When I select an item in the combo & click the button I get a value of 0, no matter which item I select.
    If I select "Short" in the combobox it should return 25000. What am I doing wrong?
    Last edited by nbrege; Jan 1st, 2024 at 10:03 AM.

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

    Re: Setting & getting Combobox value member

    Code:
    Dim dt As New DataTable
    dt.Columns.Add(“key”)
    dt.Columns.Add(“value”, GetType(integer))
    
    dt.Rows.Add("Quick", 10000)
    dt.Rows.Add("Short", 25000)
    dt.Rows.Add("Medium", 50000)
    dt.Rows.Add("Long", 100000)
    Me.ComboBox1.DisplayMember = "key"
    Me.ComboBox1.ValueMember = "value"
    Me.ComboBox1.DataSource = dt

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Setting & getting Combobox value member

    What .paul. has demonstrated but not explained is that, in order for the ValueMember to be useful, you must bind data to the ComboBox, i.e. you must set the DataSource rather than populating the Items collection directly. You can bind any appropriate list so you could use a DataTable as shown or you could populate a Dictionary instead of creating KeyValuePairs directly and bind that. You could also bind a list of tuples.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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

    Re: Setting & getting Combobox value member

    Quote Originally Posted by jmcilhinney View Post
    …you could populate a Dictionary instead of creating KeyValuePairs directly and bind that. You could also bind a list of tuples.
    With a Dictionary or a List of Tuples, how would you set DisplayMember and ValueMember?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Setting & getting Combobox value member

    Quote Originally Posted by .paul. View Post
    With a Dictionary or a List of Tuples, how would you set DisplayMember and ValueMember?
    The DisplayMember and ValueMember need to be set to the names of PropertyDescriptors. With a DataTable, those names come from the DataColumns and with a collection they come from the item properties. In a Dictionary, the items are KeyValuePairs, so the property names are Key and Value. For a list of Tuples, the property names are Item1, Item2, etc.

    That said, data binding requires an IList or IListSource and a Dictionary only implements ICollection. You would have to call ToList or ToArray on the Dictionary to create an IList to assign to the DataSource, e.g.
    Code:
    Dim list1 As New Dictionary(Of String, Integer) From {{"First", 1}, {"Second", 2}}
    
    With ComboBox1
        .DisplayMember = "Key"
        .ValueMember = "Value"
        .DataSource = list1.ToArray()
    End With
    In the case of a list of Tuples, that would be an IList, so you can assign it directly, e.g.
    Code:
    Dim list2 = {Tuple.Create("First", 1), Tuple.Create("Second", 2)}
    
    With ComboBox2
        .DisplayMember = "Item1"
        .ValueMember = "Item2"
        .DataSource = list2
    End With
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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

    Re: Setting & getting Combobox value member

    Thanks

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Setting & getting Combobox value member

    Thanks for the comments. I was missing the data binding part. I actually ended up using Bing AI to get the following working code:

    Code:
            Dim items As New List(Of KeyValuePair(Of String, Integer))()
            items.Add(New KeyValuePair(Of String, Integer)("Quick", 15000))
            items.Add(New KeyValuePair(Of String, Integer)("Short", 50000))
            items.Add(New KeyValuePair(Of String, Integer)("Medium", 125000))
            items.Add(New KeyValuePair(Of String, Integer)("Long", 250000))
            Me.myComboBox.DataSource = items
            Me.myComboBox.DisplayMember = "Key"
            Me.myComboBox.ValueMember = "Value"

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Setting & getting Combobox value member

    You should be setting the DataSource last, after the DisplayMember and ValueMember. It will still work otherwise but it is preferable to do it that way. That's because the binding is done when the DataSource is set and, if you set DisplayMember and ValueMember after that, the binding has to be redone. It's possible that the user will see the effects of that.

    Also, you should NOT be adding directly to the Items collection. You either do that OR set the DataSource, not both.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Setting & getting Combobox value member

    Quote Originally Posted by jmcilhinney View Post
    Also, you should NOT be adding directly to the Items collection. You either do that OR set the DataSource, not both.
    I'm not sure what you mean by this. Please clarify. I'm adding items to a list called "items", not to the combobox .Items collection...
    Last edited by nbrege; Jan 1st, 2024 at 04:27 PM.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Setting & getting Combobox value member

    Quote Originally Posted by nbrege View Post
    I'm not sure what you mean by this. Please clarify. I'm adding items to a list called "items", not to the combobox .Items collection...
    So you are. Sorry, my mistake.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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