Member_2_99151
asked on
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:
Any help would be appreciated...
Best regards,
James
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'
but when I select the item, I want a 'hidden' field to be available, such as:'R'
'B'
'G'
Ideally I would like to do something like:MyCombo.AddItem('Red', 'R');
MyCombo.AddItem('Green', 'G');
MyCombo.AddItem('Blue', 'B');
and be accessed:HiddenValue := MyCombo.Items[MyCombo.SelIndex].Value;
Any ideas how I can achieve something resembling this?Any help would be appreciated...
Best regards,
James
ASKER
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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'
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'
ASKER
Ah!
I can owner draw the ComboBox:
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;
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...
Retrieve data using current combobox item data...
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';
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];
Using OwnerDraw is also possible, but keep in mind to set style property of your combobox to csOwnerDrawFixed or csOwnerDrawVariable to activate OnDrawItem event...
ASKER
Thanks for the help :-)
table = new datatable
table.columns.add("descrip
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.