Solved

I want a ComboBox like SELECT in HTML

Posted on 2003-10-23
12
220 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now