Solved

I want a ComboBox like SELECT in HTML

Posted on 2003-10-23
12
223 Views
Last Modified: 2010-04-03
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
0
Comment
Question by:BarCode99
  • 6
  • 4
  • 2
12 Comments
 
LVL 26

Accepted Solution

by:
EddieShipman earned 50 total points
ID: 9607686
Get the ItemIndex of the ComboBox. That is the "key" in the list of items.
Remember, they begin at 0.
0
 
LVL 27

Assisted Solution

by:kretzschmar
kretzschmar earned 50 total points
ID: 9607697
you may use the itemIndex-property (maybe +1) instead

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9607708
too late :-(
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:BarCode99
ID: 9607741
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
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9607810
>Can I associate the ID with the text somehow?
nope

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

sample follows

meikl ;-)

0
 

Author Comment

by:BarCode99
ID: 9607824
So what is the Data pointer for in a TComboBoxEx item?

Marius
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9607877
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 ;-)
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 9607966
Now, if you'd pointed that out to us in the beginning when you asked the question....
0
 

Author Comment

by:BarCode99
ID: 9608059
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
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9608320
>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 ;-)
0
 

Author Comment

by:BarCode99
ID: 9608381
Spilt the point as Eddie was first, but kretzschmar help more...

Hope you don't mind.

Marius
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9608468
is ok ;-)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

839 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