Combo Box 2 values

  • Thread starter Thread starter John S
  • Start date Start date
J

John S

I need to Show one value in the combo box, but when it is clicked, I need to
save it's corresponding value.
Example:
Show: United States
Retrieve when clicked: USA
 
Hi Jon,

You can add whatever class you want to combobxes. What you see is waht
ToString method returns

So you can have type

class Country
{
public string Display;
public string Value;

public override string ToString()
{
return Display;
}
}

you can add objects of this type to a combobx. It will show what is in
Display filed and when you it is selected and read it you can use the Value
field.

If you don't want to override ToString or you can't a solution canbe to use
combobx's DisplayMember property

Even though you don't use datasource you can still use DisplayMember
property. You don't need to set ValueMember at all. But if you want to you
can set it to whatever you want (for example you can set it to the same
property as DisplayMember). When the selection changes use SelectedIndex or
SelectedItem properties. Don't use SelectedValue it seems like this property
doesn't work without datasource.

Make sure that you use name of a *property* (not a member variable) for
DisplayMember. In the example I gave you for the Country class Display and
Value has to be turned to porperties and ToString is not necessary anymore.
Make also sure that that proerty can be accessed for reading
(it has to be visible - public) If reading the property failed in some
reason (exception thrown, mistyped property name or so) the listbox control
will use item's ToString method to fill out the combobox.
 
Back
Top