To be able to refer to instantiated controls (buttons or whatever) in code you can use at least these means:
- refer to the controls by reference (storing the references as pointers in an array or list at creation)
- refer to the controls by Name (you can store the Names in an array or list or some other container, or you can rely on the LCL's internal Controls or Components array - but this string-based lookup is slow for large numbers of controls; and you have to take care that each name is unique if you create and name the controls dynamically)
- refer to the controls by Tag. This is just a variation on using Name, but quicker to index/locate, and the same caveat applies to make each Tag unique
- add some fresh property or field to the control as an ID, and ignore the above built-in means
Of course you can combine any of these methods, as well.
Give headaches to all guys in the forum can be considered a new development style? LOL
Just laughing out here, you guys really bless my days.
In the end I can't find what I was looking for in the begging, but thanks to all the replies and ideas and codes, I figure out a way to do what I want, was far away from my initial thinking, but fits like a glove, works like a charm or any other sweet expression that can be used like a "thanks".
I mixed up almost all replies and get this code, the exactly (result) of what I want, need, etc.
Once more thanks very much for all support and patience with my lack of knowledge in english.
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls;
type
pArray = Array of TPanel;
bArray = Array[0..2] of Array[1..6] of TButton;
{ TButtonPanel }
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
p1: TPanel;
p2: TPanel;
p3: TPanel;
p4: TPanel;
procedure FormCreate(Sender: TObject);
private
{ private declarations }
procedure CreateButtons;
procedure myButtonClick(Sender:TObject);
procedure theClick(btn:TButton);
public
{ public declarations }
b:bArray;
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
CreateButtons;
end;
procedure TForm1.CreateButtons;
var
i, h, c:Integer;
pk:TPanel;
panelArray : pArray;
begin
panelArray:=panelArray.Create(p1, p2, p3);
c := Low(panelArray);
for pk in panelArray do begin
h := panelArray[c].Top;
//
for i:=Low(b[0]) to High(b[0]) do begin
b[c][i]:=TButton.Create(Self);
with b[c][i] do begin
Name:=panelArray[c].Name+'_'+IntToStr(i);
Caption:=Name;
Parent:=panelArray[c];
Tag:=c;
Top:=h;
Width:=panelArray[c].Width;
//
OnClick:=@myButtonClick;
end;
h:=h+30;
end;
c:=c+1;
//
end;
end;
procedure TForm1.myButtonClick(Sender: TObject);
var
bc:TButton;
begin
bc := Sender as TButton;
theClick(bc);
end;
procedure TForm1.theClick(btn: TButton);
var
i:Integer;
begin
p4.Caption:='Nome: '+btn.Name+' TAG: '+IntToStr(btn.Tag);
for i:=Low(b[0]) to High(b[0]) do begin
b[btn.Tag][i].Font.Style:=[];
end;
btn.Font.Style:=[fsBold, fsItalic];
end;
end.
How can I mark like: SOLVED?