Listbox with String Key lazarus / 8-Other ---------------------------------------------------------------------- BenJones · 2013-05-25 18:08 With a listview control in VB I remember when you added items you could also add a key, but in a list box this is not possible unless you do a little coding, anyway I remember I needed something in VB.NET for adding keys to a normal listbox and combo box, and got around the problem by doing a tiny class and added it as an object. Anyway I thought why not try and do the same thing in lazarus as you can also add objects. Anyway this is what Came up with a tiny bit of code to allow you to also add key to list box, my key is added as string but you can change the data type if you like. Hope this maybe of some help to someone. Start a new project. Next we create the smalll class that will house our value and key add a unit to your project call it LstItem Add the code below to the class Code: [Select]unit LstItem; {$mode objfpc}{$H+} interface uses   Classes, SysUtils; type   TLstItem = class   private     fName, fKey: string;   public     constructor Create(const Caption, Key: string);     property Caption : string read fName;     property Key : string read fKey;   end; implementation constructor TLstItem.Create(const Caption,Key : string); begin    fName := Caption;    fKey := Key; end; end. Ok add the class to your form uses Next add a listbox to the form, use this code below that will add a new item to the listbox. Code: [Select]ListBox1.Items.AddObject('Apples', TLstItem.Create('Apples', 'Fruit')); As you can see we used our class to enable us to have a key Fruit To get the key value of a selected item just use this code: Code: [Select]procedure TForm1.ListBox1Click(Sender: TObject); var   t: TLstItem; begin   t := ListBox1.Items.Objects[ListBox1.ItemIndex] as TLstItem;   //Show caption and key.   ShowMessage(t.Caption);   ShowMessage(t.Key); end; ---------------------------------------------------------------------- howardpc · 2013-05-25 20:49 I don't think your TLstItem class needs the FName field. You're duplicating storage already provided in the the Listbox's Items, doubling memory use unnecessarily. Another approach would be to introduce a class helper for TListBox as in the following example in which a button and a listbox are dropped onto the main form: Code: [Select]unit Unit1; {$mode objfpc}{$H+} interface uses   Forms, Dialogs, StdCtrls; type   { TListBoxKeysHelper }   TListBoxKeyHelper =  class helper for TListBox   private     function GetKey(anItem: integer): string;     public       procedure SetKey(anItemIndex: integer; const aKey: string);       property Key[anItem: integer] : string read GetKey;     end;   { TForm1 }   TForm1 = class(TForm)     btnSetListBoxKey: TButton;     ListBox1: TListBox;     procedure btnSetListBoxKeyClick(Sender: TObject);     procedure FormCreate(Sender: TObject);   end; var   Form1: TForm1; implementation { TForm1 } procedure TForm1.btnSetListBoxKeyClick(Sender: TObject); begin   ListBox1.setkey(0, '1');   ListBox1.setkey(1, '2');   ListBox1.setkey(1, '3');   ShowMessageFmt('Item[1] has key %s',[ListBox1.Key[1]]); end; procedure TForm1.FormCreate(Sender: TObject); begin   ListBox1.Items.AddStrings(['one','two','three']); end; { TListBoxKeysHelper } function TListBoxKeyHelper.GetKey(anItem: integer): string; begin   Result:= '';   if Items.Count=0 then Exit;   if Items.Objects[anItem] <> nil    then Result := string(Items.Objects[anItem]); end; procedure TListBoxKeyHelper.SetKey(anItemIndex: integer; const aKey: string); begin   if (anItemIndex > -1) and (anItemIndex < Items.Count)    then Items.Objects[anItemIndex] := TObject(akey); end; {$R *.lfm} end. ---------------------------------------------------------------------- BenJones · 2013-05-26 02:08 Thanks for the heads up howardpc, Thanks for example to, I keep it in my sniplet manager.