Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

I want a ComboBox like SELECT in HTML

Posted on 2003-10-23
12
Medium Priority
?
229 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
12 Comments
 
LVL 26

Accepted Solution

by:
EddieShipman earned 200 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 200 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

636 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