Combo box values?

  • Thread starter Thread starter Rob Meade
  • Start date Start date
R

Rob Meade

Hi all,

I have come from the web arena and I'm used to having a drop down menu that
has some text values (names), and some vales (id's perhaps)...

When I've added a combo box on a Win form I only see the names, ie the text
to display, I dont see anyway (in Visual Studio / properties etc at least)
to give each one of those a value?

I hope that I am wrong when I say - can you only have the text names, and
then have to use those as values?

Any info on this would be appreciated, perhaps a small example, or a link to
an online example.

Regards

Rob
 
The Winforms ComboBox is actually a lot more flexible than the HTML <select>. In Winforms you can add any type of object to your combobox using ComboBox.Items.Add (you can not do this in the designer). The propert ComboBox.DisplayMember tells the ComboBox which property of the added objects it should use for display. A simple example:

class ComboBoxElement
{
public ComboBoxElement(string text, int value)
{
this.Text = text;
this.Value = value;
}
public string Text;
public int Value;
}

// ...
// Initialize existing combobox

comboBox1.Items.Add(new ComboBoxElement("Display1", 1));
comboBox1.Items.Add(new ComboBoxElement("Display2", 2));
comboBox1.DisplayMember = "Text";

Regards, Jakob.
 
Back
Top