Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

combobox valuemember & datamember

Status
Not open for further replies.

HomerJS

Programmer
Jun 25, 2001
86
0
0
US
I have the following code inside a buttons click event. How can I get the value of .valuemember? The messageboxes from the SelectedIndexChanged events display blank.

Dim i As Long
For i = 0 To 3
With cbo1
.DisplayMember = "value" & CStr(i + i)
.ValueMember = CStr(i + i)
.Items.Add("value" & CStr(i + i))
End With

With ListBox1
.DisplayMember = "value" & CStr(i + i)
.ValueMember = CStr(i + i)
.Items.Add("value" & CStr(i + i))
End With
Next


Private Sub cbo1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbo1.SelectedIndexChanged
MsgBox(cbo1.SelectedValue)
End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
MsgBox(ListBox1.SelectedValue)
End Sub
 
I use:

Private Sub cboCropID_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboCropID.SelectedValueChanged

Try
If IsNothing(cboCropID.SelectedValue) Then
cboCropID.SelectedIndex = 0
End If
Messagebox.Show(cbo1.Selected.Value.ToString)
Catch oEx As Exception
MessageBox.Show(oEx.Message)
Finally
'Additional code
End Try

End Sub

and haven't had a problem, so far.
 
I'm using this code in a windows application. Selected is not a member of the combobox. I'm assuming you meant selectedvalue.

However, I copied the code, changing the name of the combobox of course, and got 'object reference not set to an instance of an object'
 
Homer,

Sorry about the typo. I was in a hurry.

When populating the combobox I set a form level boolean variable in order to bypass the SelectedValueChanged event.

Private Sub LoadCrops()
'Ignore combo SelectedValueChanged event
mbolIgnore = True
'Load data in combo
With cboCropID
.DataSource = ds.Tables("Crops")
.ValueMember = "szCropID"
.DisplayMember = "szCropID"
.SelectedIndex = -1
End With
mbolIgnore = False
End Sub

Private Sub cboCropID_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboCropID.SelectedValueChanged
'Don't process when loading combo or editing records
If mbolIgnore Then Exit Sub

Try
If IsNothing(cboCropID.SelectedValue) Then
cboCropID.SelectedIndex = 0
End If
MessageBox.Show(cboCropID.SelectedValue.ToString)
Catch oEx As Exception
MessageBox.Show(oEx.Message)
Finally

End Try
End Sub

If this doesn't work please indicate which line of your code is causing the error.
 
In the SelectedValueChanged event the MessageBox.Show(cboCropID.SelectedValue.ToString) statement is causing the 'object reference ...' error. I get that error twice.
 
Your problem has to do with the fact that you're using the Items collection and not the DataSource property. Without setting the DataSource property DisplayMember and ValueMember are invalid the way you're using them.

As I'm still fairly new to .Net I haven't used the Items collection of a combo yet. So maybe one of the experts can help.
 
Thank you for your help. My only other thing is why it pops up with a few message boxes as it's setting the dataset to the combobox. I bound the combobox to a dataset and it's happier now. Happier being that: (taken directly from .NET help) 'If DropDownStyle is set to ComboBoxStyle.DropDownList, the [selectedtext] is an empty string ("").'

'Fun' with Microsoft continues . . .
 
Thanks for sharing what you found out. I wasn't aware of that.
 
Hi,

I am assigning a drop-down list with displaymember and valuemember property. User can select multiple items from this drop-down list.

With lstCountry
.DataBindings.Add(New System.Windows.Forms.Binding("SelectedValue", DsPersonal, "Person.CountryId"))
.DataSource = DsPersonal.Country
.DisplayMember = "CountryName"
.ValueMember = "CountryId"
End With

Now I want to access all the valuemember item of all the selected. I want to do this using for..next OR for..each loop..

Can anybody he in doing this.. ?

How to access the respective valuemember for a particular index ?

Thnx a lot for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top