How to create combobox with two columns (one hidden) in Delphi 7?

How to create a TComboBox with two columns that have one of its columns so that it can store the id value along with the actual element in it? And then how to get this id value programmatically?

+7
source share
2 answers

There is no need for two columns.

You can take advantage of the fact that TComboBox.Items (like many other things in Delphi, such as TStringList , TMemo.Lines and TListBox.Items ) comes from TStrings , which has both Strings and Objects properties. Objects stores anything the size of a TObject , which is a pointer.

This means that you can save the integer value by simply assigning it a TOBject when adding it and returning it to Integer when receiving it.

Something like this should work:

 procedure TForm1.FormCreate(Snder: TObject); var i: Integer; sItem: String; begin for i := 0 to 9 do begin sItem := Format('Item %d', [i]); ComboBox1.Items.AddObject(sItem, TObject(i)); end; end; 

To get the value:

 procedure TForm1.ComboBox1Click(Sender: TObject); var Idx: Integer; Value: Integer; begin Idx := ComboBox1.ItemIndex; if Idx <> -1 then begin Value := Integer(ComboBox1.Items.Objects[Idx]); // Do something with value you retrieved end; end; 

Please note: since the Objects property is actually intended to hold objects, this gives you more flexibility. Here is an example (intentionally very trivial) of storing customer contact information in the corresponding instance of the object and displaying it in shortcuts when an item is selected from the list.

 unit Unit1; interface uses Windows, Messages, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TCustomer=class private FContact: string; FPhone: string; public constructor CreateCustomer(const AContact, APhone: string); property Contact: string read FContact write FContact; property Phone: string read FPhone write FPhone; end; TForm1 = class(TForm) ListBox1: TListBox; Label1: TLabel; Label2: TLabel; Label3: TLabel; lblContact: TLabel; lblPhone: TLabel; procedure FormCreate(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure ListBox1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} { TCustomer } constructor TCustomer.CreateCustomer(const AContact, APhone: string); begin inherited Create; FContact := AContact; FPhone := APhone; end; procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); var i: Integer; begin for i := 0 to ListBox1.Items.Count - 1 do ListBox1.Items.Objects[i].Free; end; procedure TForm1.FormCreate(Sender: TObject); begin lblContact.Caption := ''; lblPhone.Caption := ''; // Create some customers. Of course in the real world you'd load this // from some persistent source instead of hard-coding them here. ListBox1.Items.AddObject('N Company', TCustomer.CreateCustomer('Nancy', '555-3333')); ListBox1.Items.AddObject('B Company', TCustomer.CreateCustomer('Brad', '555-1212')); ListBox1.Items.AddObject('A Company', TCustomer.CreateCustomer('Angie', '555-2345')); end; procedure TForm1.ListBox1Click(Sender: TObject); var Cust: TCustomer; begin if ListBox1.ItemIndex <> -1 then begin Cust := TCustomer(ListBox1.Items.Objects[ListBox1.ItemIndex]); lblContact.Caption := Cust.Contact; lblPhone.Caption := Cust.Phone; end; end; end. 
+9
source

ComboBox controls do not support columns, and you do not need a hidden column to accomplish what you need.

The TStrings property is a descendant of TStrings . It can simultaneously store both string values โ€‹โ€‹and data values โ€‹โ€‹associated with them, but the user will only see string values โ€‹โ€‹in the user interface. Use the Items.AddObject() method to add the string + id values โ€‹โ€‹to the list, and then use the Items.Objects[] property to get the id values โ€‹โ€‹when necessary.

Alternatively, you can simply store your identification values โ€‹โ€‹in a separate array with the same number of elements as the ComboBox, and then use the indexes of the ComboBox elements to access the values โ€‹โ€‹of the array. This is especially important if you need to keep the value -1, because this particular value cannot be restored from the Objects[] property of the TComboBox because of the getter method, for example, Rob.

+9
source

All Articles