Question Issues with populating a combobox with value and text

Behumble

Member
Joined
Oct 7, 2012
Messages
6
Programming Experience
5-10
Hi ,

I am trying to so something really simple but cant seem to get it right. Below is what i am trying to acheive

1) Loop through an object (not a datatable) and add items to a combobox.
Do While Not pRow Is Nothing
cmbbox.Items.Add(pRow.Value(pRow.Fields.FindField(strfieldname)))
pRow = pFCur.NextRow
Loop

The above works fine but the issues arises when i want to add value and text to combobox, and then have one value as a selected value.

Private Sub loadcombobox(ByVal strtablename As String, ByRef cmbbox As System.Windows.Forms.ComboBox, ByVal strfieldname As String, ByVal strvalue As String, ByVal selectedvalue As String)

'
'
'
'
Dim tempcombo as New ComboBox

Do While Not pRow Is Nothing
tempcombo.ValueMember = (pRow.Value(pRow.Fields.FindField(strvalue))).ToString()
tempcombo.DisplayMember = (pRow.Value(pRow.Fields.FindField(strfieldname))).ToString()
cmbbox.Items.Add(tempcombo)

pRow = pFCur.NextRow
Loop
'
'
'

If Not selectedtext Is String.Empty Then
For i = 0 To cmbbox.Items.Count - 1
If cmbbox.Items(i).ValueMember = selectedvalue Then
cmbbox.SelectedIndex = i
Exit For
End If
Next
'
'
'
'
'
End Sub

I have pasted the code that is causing trouble. can someone please help.

All i am trying to do is populate combobox with multiple items having both valuemember and displaymember and then have one of those selected. Any help is appreciated.

Thanks.

 
You're using DisplayMember and ValueMember all wrong. That code is trying to add a bunch of ComboBoxes to a ComboBox, which is not going to happen.

Firstly, ValueMember is only when the ComboBox is data-bound, so you MUST set the DataSource. Secondly, both DisplayMember and ValueMember do not accept values from the items, but rather the names of properties or columns of the items from which the values will come. For instance, if you have a DataTable with columns named ID and Name then you can do this:
With myComboBox
    .DisplayMember = "Name"
    .ValueMember = "ID"
    .DataSource = myDataTable
End With
The user will then see a list of values from the Name column and, when they make a selection, the corresponding value from the ID column is exposed via the SelectedValue property. Similarly, if you had a class like this:
Public Class Thing
    Public Property ID As Integer
    Public Property Name As String
End Class
and you had instances of that class in an array or collection, you could do this:
With myComboBox
    .DisplayMember = "Name"
    .ValueMember = "ID"
    .DataSource = myThingList
End With
and you'd see similar behaviour.

So, if your pFCur object implements the IList interface and its items have values that you can access via property descriptors then you can simply bind it like that. I'm guessing that it's not an IList though. In that case, you'll need to use a loop similar to what you have now but, instead of adding items to the ComboBox, you'll add them to a generic List. On each iteration you will create an object with two properties. It can be an object of an anonymous type or you can declare a class like I did above. At the end of the loop you'll have a List full of of objects and you can bind that to the ComboBox.
 
Thanks a lot. Sorry i was doing everything wrong. I am a newbie in VB.NET but thanks for giving me the right direction. I am sure i can move ahead with the code now.

Cheers.
 
Back
Top