procedures for 2D array of Label lazarus / 39-Graphics ---------------------------------------------------------------------- R2 · 2014-02-15 19:06 Hello for everyone! I am new in Lazarus, and I am trying to create an application, that creates 2D array of TLabel in Form and changes the caption for label, if it is clicked. I have no problems with Label creating, and no ideas about label[x,y].caption changing. Any ideas? ---------------------------------------------------------------------- Jkey · 2014-02-15 19:53 Hello and welcome! I show you an example, I hope it will fit your needs: 1. First of all, create a custom common procedure for the label clicks. Insert this into your form's private declarations: procedure MyLabelClick(Sender: TObject); 2. After that, describe the details in the implementation section: procedure TForm1.MyLabelClick(Sender: TObject); begin   TLabel(Sender).Caption := IntToStr(TLabel(Sender).Tag); // caption := tag end; 3. After that, assign your custom click event to every labels you created: Label[x,y].OnClick := @MyLabelClick; 4. Insert values to the tags: Label[x,y].Tag := x + 100 * y; // only an example ---------------------------------------------------------------------- wp · 2014-02-15 21:16 Quote label[x,y].caption For this to work you need a 2D array of labels. Declare that in the private section of your form declaration and assign the individual labels to the array elements in the OnCreate event of the form: Code: [Select]type   TForm1 = class(TForm)   ...   private     Labels: array of array of TLabel;   ... procedure TForm1.FormCreate(Sender:TObject); begin   SetLength(Labels, 10, 5);  // assuming there are 10x5 labels   Labels[0, 0] := Label1;   Labels[1, 0] := Label2;   Labels[2, 0] := Label3;   ...   Labels[9, 0] := Label10;   Labels[0, 1] := Label11;   // etc. end; ---------------------------------------------------------------------- howardpc · 2014-02-15 21:27 Here's a smple example using a static array and dynamic label creation (rather than label creation in the Designer referenced from a dynamic array). Code: [Select]unit Unit1; {$mode objfpc}{$H+} interface uses   SysUtils, Forms, StdCtrls; type   TRow = 0..9;   TCol = 0..4;   TLArray = array[TRow, TCol] of TLabel;   { TForm1 }   TForm1 = class(TForm)     procedure FormCreate(Sender: TObject);   private     FLArray: TLArray;     procedure InitLArray;     procedure LabelOnClick(Sender: TObject);   end; const   margin=10; var   Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject); begin   InitLArray; end; procedure TForm1.InitLArray; var   r, c: integer; begin   for r:= Low(TRow) to High(TRow) do     for c:= Low(TCol) to High(TCol) do begin       FLArray[r,c]:= TLabel.Create(Self);       with FLArray[r,c] do begin         Left:=c*52+margin;         Top:=r*20+margin;         Caption:=Format('Label %d:%d', [r,c]);         Color:=$808040 or ((r+1) * (c+1) * $154321);         OnClick:=@LabelOnClick;         Parent:=Self;       end;     end; end; procedure TForm1.LabelOnClick(Sender: TObject); begin   if Sender is TLabel then     TLabel(Sender).Caption:='Clicked'; end; end. ---------------------------------------------------------------------- R2 · 2014-02-16 19:40 Thanks for ideas! Application is working