Link to home
Start Free TrialLog in
Avatar of BarCode99
BarCode99

asked on

I want a ComboBox like SELECT in HTML

This should be easy, but I haven't got time to find the right/best way of doing it.

I have a dropdown (TComboBox or TComboBoxEx) that i want to populate with data (key/value pairs), but rather than having it return the text the user selects I want to return the key associated with it. (This is just like a <SELECT> with <OPTION VALUE=1>Text etc. works in HTML).

What's the best approach? TComboBoxEx using the Data pointer? If so, how?

Please give an example.

Cheers,
Marius
ASKER CERTIFIED SOLUTION
Avatar of Eddie Shipman
Eddie Shipman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
too late :-(
Avatar of BarCode99
BarCode99

ASKER

OK, but the ID is not sequential. Say I have a list like this:

5=Company A
125=Company B
45=Company C
4500=Company D
etc...

Can I associate the ID with the text somehow?

Marius
>Can I associate the ID with the text somehow?
nope

but you could use the name/value methods of the TStrings

sample follows

meikl ;-)

So what is the Data pointer for in a TComboBoxEx item?

Marius
well, a simple sample

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    ComboBox1: TComboBox;
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

//populate Combo - listbox1 holds the data
procedure TForm1.Button1Click(Sender: TObject);
var i : Integer;
begin
  combobox1.Items.clear;
  for i := 0 to Listbox1.Items.Count - 1 do
    combobox1.Items.Add(ListBox1.Items.ValueFromIndex[i]);
end;

//display the result in edit1
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
  edit1.Text := ListBox1.Items.Names[ComboBox1.ItemIndex];

end;

end.

meikl ;-)
Now, if you'd pointed that out to us in the beginning when you asked the question....
EddieShipman,
Well, I hoped giving the hint that it should work like the HTML SELECT would help (Yes I know this is not the HTML topic area.) I'll try to be more precise the next time....

kretzschmar,
Do you not think just holding an array of integers containing the associated ID would be easier? Like this

i: integer;
aIDs : array of integer;
i := 0;
while not eof then
  combobox1.Items.Add(value);
  aIDs[i] := key;
  Inc(i);
  Next;
end

Marius
>Do you not think just holding an array of integers containing the
>associated ID would be easier?

i don't know how you have your data,
if this will be easier for you,
then just do this

meikl ;-)
Spilt the point as Eddie was first, but kretzschmar help more...

Hope you don't mind.

Marius
is ok ;-)