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?
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
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.
Re: Setting & getting Combobox value member
Quote:
Originally Posted by
jmcilhinney
…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?
Re: Setting & getting Combobox value member
Quote:
Originally Posted by
.paul.
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
Re: Setting & getting Combobox value member
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"
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.
Re: Setting & getting Combobox value member
Quote:
Originally Posted by
jmcilhinney
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...
Re: Setting & getting Combobox value member
Quote:
Originally Posted by
nbrege
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.