Create an array of components

Posted in Components

First of all, you need to declare the array. Then, if you will create the Button components dynamically, you may execute procedure BitBtn1Click. Then, if you will create the Button components static, you may execute procedure BitBtn2Click.

var
  Edits : array[1..5] of TEdit;
  SPButtons: array[1..5] of TSpeedButton;

implementations

procedure TForm1.BitBtn1Click(Sender: TObject);
var
  i: Integer;
begin
  for i:=1 to 5 do
  begin
    Edits[i]:=TEdit.Create(Form1);
    Edits[i].Parent:= Form1;
    Edits[i].Left:=40;
    Edits[i].Top:=Form1.Height-(5-i)*40-100;
    Edits[i].Text:='Edit '+IntToStr(i);
  end;
end;

procedure TForm1.BitBtn2Click(Sender: TObject);
var
  Btns, Counter: Integer;
begin
  Btns:=0;
  for Counter:=0 to Form1.ComponentCount-1 do
  begin
    if (Components[Counter] is TSpeedButton) and (Btns<5) then
    begin
      Inc(Btns);
      SPButtons[Btns]:=TSpeedButton(Components[Counter]);
      SPButtons[Btns].Caption:='SP'+IntToStr(Btns);
    end
  end;
end;

Related chapters
    Databases