Jednoczesne nadanie kilku parametrów wielu komponentom

0

Czy jest to możliwe? Mam zestaw parametrów Width, Height, Left, Top, Font.Size, Font.Color i jeszcze kilka innych. One mają mieć określoną wartość, a więc mogę je zapisać do chociażby jako zmienne.
Do tego mam 3 Panele, które te parametry mają mieć takie same. Czy jest możliwość zrobienia tego jakoś automatycznie czy trzeba wszystko przepisywać?

 
Panel1.Width := PanelWidth;
Panel2.Width := PanelWidth;
Panel3.Width := PanelWidth;
Panel1.Left := PanelLeft;
...
1

FindComponent w pętli.

3

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;
0

Możesz też i tak:


type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
   {...}
  private
    procedure SetPanelDefaultProps(const aPanel:TPanel); //domyslne, wspolne ustawienia dla TPanel
    procedure FindAndSetPanelDefaultProps(const aControl:TWinControl);
  public
   {...}
  end;


{...}

procedure TForm1.Button1Click(Sender: TObject);
begin
 FindAndSetPanelDefaultProps(self); //szukanie Panel'i zaczynamy od Formy; mozesz rowniez zaczac od innego componentu ktory jest na formie
end;

procedure TForm1.FindAndSetPanelDefaultProps(const aControl: TWinControl);
var i:integer;
begin
 //jak przekazany obiet to Panel to ustawiamy dla niego wartosci domyslne
 if aControl is (TPanel) then
  SetPanelDefaultProps(TPanel(aControl));

 //jak controlka to kontener(posiada controlki zagneizdzone) to szukamy czy tam nie ma w nim Panel'i
 for i:=0 to Pred(aControl.ControlCount) do
  if aControl.Controls[i] is TWinControl then
   FindAndSetPanelDefaultProps(TWinControl(aControl.Controls[i]));
end;

procedure TForm1.SetPanelDefaultProps(const aPanel: TPanel);
begin
  //tu wstawiasz to co chcesz ustawic dla Panel'i

 aPanel.Width:=20;
 aPanel.Left:=20;
 aPanel.Font.Color:=clRed;

end;

1 użytkowników online, w tym zalogowanych: 0, gości: 1