Link to home
Start Free TrialLog in
Avatar of BlakeMcKenna
BlakeMcKennaFlag for United States of America

asked on

Retrieving SelectedValue from ComboBox programmatically?

Ok,

Here is a strange request but I am not really sure how to do this. I have a ComboBox (CB) that has two items in it. It's a DataBound CB so it has ValueMember/DisplayMember properties set. There is a default value selected. When certain processing is done within the app, the SelectedValue needs to change to the other Item in the CB without the user manually selecting it. Is there a way of Looping through the CB's Items?

Thanks!
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

you can loop through the items property
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BlakeMcKenna

ASKER

I do have the other ValueMember, however, I want to keep it as dynamic as possible. I'd rather not hard-code anything because the ValueMember values come from a Database.
I don't mean to hardcode. You can surely retrieve the second value and use it to set the selectedvalue property. You could also set the selectedindex property which is the position
Eric,

I found a solution. I actually add the ValueMember (the primary key) to an array variable when the CB was first loaded. At the point when I need to change the value, I simply looped thru the 2 element array and chose the other value. It's rude and crude but it works.

Thanks for your posts!
Thanks again for your input!
Is the combobox the ideal solution if you have only 2 values? wouldn't 2 option buttons be better?
Using Option Buttons would make sense with the way things are now, however, since it is database driven, it is conceivably possible for there to be more than two values. If that were the case, then I would need to create these controls dynamically on the form and unfortunately the real estate is pretty tight.