Solved

I want a ComboBox like SELECT in HTML

Posted on 2003-10-23
12
219 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
Comment Utility
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
Comment Utility
you may use the itemIndex-property (maybe +1) instead

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
too late :-(
0
 

Author Comment

by:BarCode99
Comment Utility
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
Comment Utility
>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
Comment Utility
So what is the Data pointer for in a TComboBoxEx item?

Marius
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
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
Comment Utility
Now, if you'd pointed that out to us in the beginning when you asked the question....
0
 

Author Comment

by:BarCode99
Comment Utility
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
Comment Utility
>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
Comment Utility
Spilt the point as Eddie was first, but kretzschmar help more...

Hope you don't mind.

Marius
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
is ok ;-)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

16 Experts available now in Live!

Get 1:1 Help Now