|
-
Dec 31st, 2023, 02:24 PM
#1
Thread Starter
Frenzied Member
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.
-
Dec 31st, 2023, 05:27 PM
#2
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 31st, 2023, 09:18 PM
#3
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.
-
Dec 31st, 2023, 10:09 PM
#4
Re: Setting & getting Combobox value member
 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?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 31st, 2023, 10:30 PM
#5
Re: Setting & getting Combobox value member
 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
-
Jan 1st, 2024, 12:29 AM
#6
Re: Setting & getting Combobox value member
Thanks
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 1st, 2024, 10:10 AM
#7
Thread Starter
Frenzied 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"
-
Jan 1st, 2024, 10:22 AM
#8
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.
-
Jan 1st, 2024, 11:05 AM
#9
Thread Starter
Frenzied Member
Re: Setting & getting Combobox value member
 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...
Last edited by nbrege; Jan 1st, 2024 at 04:27 PM.
-
Jan 1st, 2024, 09:32 PM
#10
Re: Setting & getting Combobox value member
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|