Solved

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

Posted on 2012-03-13
9
384 Views
Last Modified: 2012-03-13
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:
'Red'
'Blue'
'Green'

Open in new window

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

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
0
Comment
Question by:jatkin
  • 4
  • 3
  • 2
9 Comments
 
LVL 9

Expert Comment

by:wellhole
ID: 37715026
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.columns.add("description")
table.columns.add("value")
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.
0
 
LVL 4

Author Comment

by:jatkin
ID: 37715073
Thanks for the suggestion but this is required for Delphi....
0
 
LVL 9

Expert Comment

by:wellhole
ID: 37715110
My mistake. This new website makes it much more difficult to tell what kind of questions I'm reading.
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 19

Accepted Solution

by:
Thommy earned 500 total points
ID: 37715112
Set your combobox items to <name>=<value>

MyCombo.AddItem('Red=R',nil);
MyCombo.AddItem('Green=G',nil);
MyCombo.AddItem('Blue=B',nil);

Open in new window




Retrieve values and names...

var
  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

0
 
LVL 4

Author Comment

by:jatkin
ID: 37715162
Thanks...
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'
0
 
LVL 4

Author Comment

by:jatkin
ID: 37715231
Ah!
I can owner draw the ComboBox:
procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
begin
    ComboBox1.Canvas.TextRect(Rect, Rect.Left, Rect.Top, ComboBox1.Items.Names[Index]);
end;

Open in new window

Is this a 'correct' way to achieve this?
0
 
LVL 19

Expert Comment

by:Thommy
ID: 37715269
if you don't want both, name and value, displayed in your combobox I suggest to use a separate stringlist...

 
  sl: TstringList;  //global var

  ComboBox1.AddItem('Red',nil);
  ComboBox1.AddItem('Green',nil);
  ComboBox1.AddItem('Blue',nil);

  //create separate stringlist with names and values derived from combobox
  sl:=TStringList.create;  
  sl.AddStrings(combobox1.Items);
  sl[0]:=sl[0]+'=R';
  sl[1]:=sl[1]+'=G';
  sl[2]:=sl[2]+'=B';

Open in new window


Retrieve data using current combobox item data...

var
  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
AName:=sl.Names[combobox1.ItemIndex];  

Open in new window

0
 
LVL 19

Expert Comment

by:Thommy
ID: 37715373
Using OwnerDraw is also possible, but keep in mind to set style property of your combobox to csOwnerDrawFixed or csOwnerDrawVariable to activate OnDrawItem event...
0
 
LVL 4

Author Closing Comment

by:jatkin
ID: 37715392
Thanks for the help :-)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 100
Delphi TcxGrid group footer summary 3 268
Delphi Yen format 3 34
I want to use librsync in my Delphi backup application. 3 49
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question