Możesz to zrobić np. tak:
type
TForm1 = class(TForm)
{..}
private
procedure SetPanelProps(AWidth, AHeight, AFontSize: Integer; AFontColor: TColor);
end;
{..}
procedure TForm1.SetPanelProps(AWidth, AHeight, AFontSize: Integer; AFontColor: TColor);
var
arrPanels: array [0 .. 2] of TPanel;
intToken: Integer;
begin
arrPanels[0] := Panel1;
arrPanels[1] := Panel2;
arrPanels[2] := Panel3;
for intToken := Low(arrPanels) to High(arrPanels) do
with arrPanels[intToken] do
begin
Width := AWidth;
Height := AHeight;
Font.Size := AFontSize;
Font.Color := AFontColor;
end;
end;
Taką metodę wystarczy wywołać w poniższy sposób:
SetPanelProps(100, 50, 14, clBlue);
A jeśli panele masz tak nazwane, jak w swoim przykładzie, to możesz też skorzystać z FindComponent
i pominąć użycie macierzy:
procedure TForm1.SetPanelProps(AWidth, AHeight, AFontSize: Integer; AFontColor: TColor);
var
intToken: Integer;
begin
for intToken := 1 to 3 do
with FindComponent(Format('Panel%d', [intToken])) as TPanel do
begin
Width := AWidth;
Height := AHeight;
Font.Size := AFontSize;
Font.Color := AFontColor;
end;
end;
Możesz też przekazać konkretne panele w parametrze metody, a informacje opakować sobie w rekord:
type
TPanelInfo = record
Width: Integer;
Height: Integer;
FontSize: Integer;
FontColor: TColor;
end;
type
TForm1 = class(TForm)
Panel1: TPanel;
Panel2: TPanel;
Panel3: TPanel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
procedure SetPanelProps(APanelInfo: TPanelInfo; APanels: array of TPanel);
end;
{..}
procedure TForm1.SetPanelProps(APanelInfo: TPanelInfo; APanels: array of TPanel);
var
intToken: Integer;
begin
for intToken := Low(APanels) to High(APanels) do
with APanels[intToken] do
begin
Width := APanelInfo.Width;
Height := APanelInfo.Height;
Font.Size := APanelInfo.FontSize;
Font.Color := APanelInfo.FontColor;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
piNew: TPanelInfo;
begin
piNew.Width := 100;
piNew.Height := 50;
piNew.FontSize := 14;
piNew.FontColor := clBlue;
SetPanelProps(piNew, [Panel1, Panel2, Panel3]);
end;