How can I store more than one piece of information in a ComboBox item?

Hi all,
Sorry if this is a stupid question, but how should I store more than one piece of information in a ComboBox Item?
For example, say I have 3 entries:

Open in new window

but when I select the item, I want a 'hidden' field to be available, such  as:

Open in new window

Ideally I would like to do something like:
MyCombo.AddItem('Red', 'R');
MyCombo.AddItem('Green', 'G');
MyCombo.AddItem('Blue', 'B');

Open in new window

and be accessed:
HiddenValue := MyCombo.Items[MyCombo.SelIndex].Value;

Open in new window

Any ideas how I can achieve something resembling this?
Any help would be appreciated...
Best regards,
James AtkinSenior Principle Software EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You could bind your combobox to a datasource (ie MyCombo.Datasource = table) and set the display and value members to the columns in the table.

table = new datatable
table.rows.add("desc1", "value1")
table.rows.add("desc2", "value2")
table.rows.add("desc3", "value3")

combo.displaymember = "description"
combo.valuemember = "value"
combo.datasource = table

When you want to get the value of the selection you would simply use MyCombo.SelectedValue.
James AtkinSenior Principle Software EngineerAuthor Commented:
Thanks for the suggestion but this is required for Delphi....
My mistake. This new website makes it much more difficult to tell what kind of questions I'm reading.
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Set your combobox items to <name>=<value>


Open in new window

Retrieve values and names...

  AValue: string;
  AName: string;

AValue :=MyCombo.Items.ValueFromIndex[0];  //returns 'R'
AValue :=MyCombo.Items.Values['Red'];  //also returns 'R'

AName:=MyCombo.Items.Names[0];  //returns 'Red'

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
James AtkinSenior Principle Software EngineerAuthor Commented:
That makes the selection filter correctly, but how do I stop the 'Red=R' from being displayed in the dropdown? It should only display 'Red'
James AtkinSenior Principle Software EngineerAuthor Commented:
I can owner draw the ComboBox:
procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
    ComboBox1.Canvas.TextRect(Rect, Rect.Left, Rect.Top, ComboBox1.Items.Names[Index]);

Open in new window

Is this a 'correct' way to achieve this?
if you don't want both, name and value, displayed in your combobox I suggest to use a separate stringlist...

  sl: TstringList;  //global var


  //create separate stringlist with names and values derived from combobox

Open in new window

Retrieve data using current combobox item data...

  AValue: string;
  AName: string;

//returns value of your current combobox item
AValue :=sl.ValueFromIndex[combobox1.ItemIndex];  

//also returns value of your current combobox item
AValue :=sl.Values[combobox1.Text];

//returns name of your current combobox item
//same as combobox1.Text

Open in new window

Using OwnerDraw is also possible, but keep in mind to set style property of your combobox to csOwnerDrawFixed or csOwnerDrawVariable to activate OnDrawItem event...
James AtkinSenior Principle Software EngineerAuthor Commented:
Thanks for the help :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.