On Mon, 19 Oct 1998 11:28:12 +1000, trevor Baillie
Quote
<ozwe...@dot.net.au> wrote:
> How is it possible to create an array of a control at run time?
There are a wide variety of ways to do this. Probably the easiest is
to use a TList. Below is a simple example.
[Note that the example implements properties named Controls and
ControlCount, which will obscure the normal TForm.Controls and
TForm.ControlCount properties. There is no harm in this, but
presumably you'll select more appropriate and meaningful property
names.]
unit Unit1;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
FControls : TList;
function GetControlCount: Integer;
function GetControls(Index: Integer): TControl;
procedure FreeControls;
public
procedure AddControl(Control: TControl);
property Controls[Index: Integer]: TControl read GetControls;
property ControlCount: Integer read GetControlCount;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
const
NumberOfControls = 3;
var
Button: TButton;
I : Integer;
begin
for I := 0 to (NumberOfControls - 1) do
begin
Button := TButton.Create(Self);
Button.SetBounds(0, I * 32, 75, 25);
Button.Parent := Self;
AddControl(Button);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FControls := TList.Create;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FreeControls;
FControls.Free;
end;
procedure TForm1.FreeControls;
var
I : Integer;
begin
for I := (ControlCount - 1) downto 0 do Controls[I].Free;
end;
procedure TForm1.AddControl(Control: TControl);
begin
FControls.Add(Control);
end;
function TForm1.GetControls(Index: Integer): TControl;
begin
Result := TControl(FControls[Index]);
end;
function TForm1.GetControlCount: Integer;
begin
Result := FControls.Count;
end;
end.
--
Rick Rogers (TeamB) | Fenestra Technologies
http://www.fenestra.com/