2008. június 12., csütörtök

Create an array of TEdit components on a TFrame


Problem/Question/Abstract:

Is it possible to have an array of components (for example TEdit) in a frame? If so, must I create them at runtime?

Answer:

You can create the components at design time, as usual. But you have to set up the array with the component references at run-time. This is quite painless if you use the default naming for the components the IDE produces, or something equivalent, with a running number at the names end:

{ ... }
private
Edits: array[1..10] of TEdit;
public

constructor Create(aOwner: TComponent); override;
{ ...  }

constructor TFrameX.Create(aOwner: TComponent);
var
  i: Integer;
  edt: TComponent;
begin
  inherited;
  for i := Low(Edits) to High(Edits) do
  begin
    edt := FindComponent('edit' + IntToStr(i));
    Assert(Assigned(edt), 'Edit' + IntToStr(i) + ' not found!');
    Edits[i] := edt as TEdit;
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése