jllydgnt
06-30-2004, 07:00 AM
How can you access the current valuemember right after a combobox selection changes? Normally(not during the change event), you can access it like this.
MessageBox.Show(Me.YourComboBox.SelectedValue)
However, I am getting an error message when I try this during the default change event. I guess it is because it is trying to read the value before it is set or something. The following will kick an error that says "Cast from type 'DataRowView' to type 'String' is not valid."
Private Sub YourComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles YourComboBox.SelectedIndexChanged
MessageBox.Show(Me.YourComboBox.SelectedValue)
End Sub
PWNettle
06-30-2004, 09:30 AM
I don't know if this helps, and I don't know the code since I'm more of a C# .Net person.
You've most likely bound some type of collection of objects to your combo. Therefore whenever you're examing the 'current item' it's an object of a specific type and 'SelectedValue' is either confused or doesn't know which property of the currently selected object to show you. You probably need to cast the 'SelectedItem' to the appropriate object type and pull the "value" from the object type. OR...
If you haven't done so, another way to handle this might be to set the ValueMember of your combo to the property from the objects in the collection you've bound that contains the value you want. (See MSDN on 'ValueMember' for details because my explanation sucks). Once you've set the 'ValueMember' so that the combo "knows" which object property to use as the value, then SelectedValue should return something you can deal with easier. For example, say you had an ArrayList of objects representing countries - and each object had a 'countryid' and 'countryname' property - you'd want to set the 'ValueMember' of a combo to 'countryid' if you bound such a collection to a combo (and probably set 'DisplayMember' to 'countryname' too) - since you're binding something that has multiple properties you need to let the combo know which to display and which to use for values. Anyways...
Good luck,
Paul
jllydgnt
06-30-2004, 09:55 AM
Thanks for the reply, PWNettle.
Yeah, I am binding this combobox to a datatable like so:
Me.MyComboBox.DataSource = Me.MyDataSet.MyDataTableName
'what I want to display
Me.MyComboBox.DisplayMember = "MyDataTableColumnName1"
'the real value I need to work with
Me.MyComboBox.ValueMember = "MyDataTableColumnName2"
So, the display and value members are definitely set. But when I try to access the valuemember from the combobox's SelectedIndexChanged event, I get the error(refer to original post for event and error).
Now, I am able to access the value with no problem when the combobox is not firing an event. Like if I chose something in the combobox and then had a seperate button that just messageboxed the valuemember or something else that did not involve a combobox event.
This leads me to believe that during the SelectedIndexChanged event, the value of valuemember is not set yet. I have also tried these events:
SelectedValueChanged
SelectionChangeCommitted
ValueMemberChanged
There must be a way to access the valuemember when a combobox's change event fires. Basically what I am saying is, "Okay, the user just changed what is selected in the combobox. Give me the new value." There must be a way to do this.
jllydgnt
06-30-2004, 12:16 PM
Oops. It is the SelectionChangeCommitted event. I had not changed the procedure's "handles" part. My bad. I was being dumber than a Republican on welfare.
rpallas
01-10-2005, 04:40 AM
I can't believe this isn't mentioned anywhere in any of the first 5-10 texts on comboboxes i searched through while trying to figure this out.
I just lost about 2 hours.