Question combobox and ojbects...

aussierules

New member
Joined
Jul 2, 2012
Messages
1
Programming Experience
1-3
Hi,
I have a combobox where I have added a number of objects :

Dim objOp4a As New clsObjectobjOp4a.ID = 0
objOp4a.Name ="NONE"
cboConfig.Items.Add(objOp4a)

Dim objOp4 As New clsObject
objOp4.ID = 1
objOp4.Name = "Put Bench"
cboConfig.Items.Add(objOp4)

Dim objOp5 As New clsObject
objOp5.ID = 8
objOp5.Name = "Sort Bench"
cboConfig.Items.Add(objOp5)


I now need to set the combobox (using vb.net code) to set its item to be the Sort bench text, but I only have the ID (8) to work with.

Thanks
 
Instead of adding the items directly to the ComboBox, put them into an array or collection and then bind that to the ComboBox:
With myComboBox
    .DisplayMember = "Name"
    .ValueMemebr = "ID"
    .DataSource = myList
End With
You can then select an item by ID by assigning the desired value to the SelectedValue property of the ComboBox.
 
Back
Top