How do I Select Combobox Value

G

Guest

I'm trying to select a value from a combobox but I'm not able to get the
combobox to change (it just stays blank) :

Me.cboDiagnosis.SelectedItem = 5

I've set the ValueMember and DisplayMember when I binded..and I know there
is a ValueMember = 5 but I can't select it...what am I doing wrong?

Thanks,
Kent.
 
L

Lee Gillie

KentG said:
I'm trying to select a value from a combobox but I'm not able to get the
combobox to change (it just stays blank) :

Me.cboDiagnosis.SelectedItem = 5

I've set the ValueMember and DisplayMember when I binded..and I know there
is a ValueMember = 5 but I can't select it...what am I doing wrong?

Thanks,
Kent.

What did you bind to? Generally SelectedItem is an object reference.
(this code is not compilable, but I think you will get the idea...)

Example:
Class CBItem
Public mDesc as String
Public mValue as Integer
Public Readonly property DispMem as string
return mDesc
End Property
Public Readonly property ValMem as integer
return mValue
End Property
Public Overrides Function ToString as string
return mDesc
End Property
End Class

Public mArry as New ArrayList
Dim cbi as CBItem
cbi = new CBItem
cbi.mDesc = "This must be the very first one"
cbi.mValue = 1
mArry.Add(cbi)
... etc

Now bind
cbMyCB.DisplayMember = "DispMem"
cbMyCB.ValueMember = "ValMem"
cbMyCB.DataSource = mArry

Now.....

you can set by index
cbMyCB.SelectedIndex = 3 ' 3rd element in your mArry
cbMyCB.SelectedItem = cbi ' Some cbi instance that is in your mArry
cbMyCB.SelectedValue = 2 ' the cbi item in your mArry with an mValue = 2

HTH - Lee
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top