Hi jv.
Why you can not belive that control can be created at runtime? :-) Graphic control is no different than any other object and as every object it can be created at runtime.
The main difference between creating an object at runtime and designtime is that at runtime you must setup some (often a lot) properties which are easier to setup in designtime (or are set up automatically by IDE).
Here is a code which will create a bunch of images at random position on the form.
var
Images: array of TImage;
procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
MAX_IMAGES: Integer;
begin
Randomize;
MAX_IMAGES := 25;
SetLength(Images, MAX_IMAGES);
for i := 0 to MAX_IMAGES - 1 do
begin
Images[i] := TImage.Create(Self);
TImage(Images[i]).Transparent := True;
TImage(Images[i]).Parent := Self;
TImage(Images[i]).Width := 32;
TImage(Images[i]).Height := 32;
TImage(Images[i]).Left := Random(Self.Width);
TImage(Images[i]).Top := Random(Self.Height);
TImage(Images[i]).Canvas.Brush.Color := clRed;
TImage(Images[i]).Canvas.Pen.Color := clNone;
TImage(Images[i]).Canvas.Ellipse(0,0,32,32);
end;
end;
I know that this is a longer piece of code compared to the VB but you may easly write a procedure wich will imitate the behaviour of VB code.
In the above example, we create a dynamic array by setting its length. The length can be assigned dynamically over the time. To free an array set it to the nil
Images := nil
Remeber that this only free the memory assigned for the array, it wont free your images. However, Images will be freed by their owner(just before the owner gets freed) which is in this example a form.
Code which I've shown is just a one way of achiving this and probably not the best. You could keep your controls in TList or TObjectList also or derive a strong typed collection. However arrays are fast and small compared to the other solutions.
In case of graphical controls you must be especialy aware of the their two properties which are Owner and Parent. Owner is responsible for freeing owned objects and parent is a control in which boundaries its children will be drawn.
Hope this was somewhat helpful
