Loop Through Components lazarus / 10-Designer ---------------------------------------------------------------------- rjmatm · 2005-06-09 18:36 I have 5 TImage components on a form. myImg1 myImg2 myImg3 myImg4 myImg5 I would like to run a for loop to do some things with them in the code much like what can be done with an array. for i := 1 to 5 do   begin   myArray := 20;   end; Is there a way to set up those image components so they can be used like that? ---------------------------------------------------------------------- matthijs · 2005-06-10 15:04 Yes. In the form definition add something like: Code: [Select]   TForm1 = class(TForm)     procedure Form1Create(Sender: TObject);     procedure Form1Destroy(Sender: TObject);   private     { private declarations }     ImageArray: Array[1..5] of TImage;   public     { public declarations }   end; And make the create and destroy events as follows: Code: [Select] procedure TForm1.Form1Create(Sender: TObject); var   I: Integer; begin   for I := 1 to 5 do begin     ImageArray[I] := TImage.Create(nil);     // Set Properties of the Image   end; end; procedure TForm1.Form1Destroy(Sender: TObject); var   I: Integer; begin   for I := 1 to 5 do     ImageArray[I].Free; end; ---------------------------------------------------------------------- Marc · 2005-06-13 12:03 If you Creqate the images with the form as owner ( ImageArray := TImage.Create(Self) )  you don't even have to frre them in your Destroy ---------------------------------------------------------------------- rjmatm · 2005-06-13 17:07 I tried the first example.  There were no errors at all, however, the images would not show on the form. I'll try setting the owner to Self and see what happens. ---------------------------------------------------------------------- matthijs · 2005-06-13 17:17 You have to set the parent as well. No need to set the owner, as long as you remember to free them on destroy. (Althought Marc is right, if you set the owner you don't need to bother to free them yourself. But I like it the hard way, what you create yourself you destroy yourself as well.) Code: [Select] ImageArray[I].Parent := Self; ---------------------------------------------------------------------- rjmatm · 2005-06-14 02:22 WOW! Thanks for the help guys!  It works great! Just to show everyone how it came together here is the code that I used that's working. Code: [Select]   TForm1 = class(TForm)   procedure Form1Create(Sender: TObject);   private     { private declarations }   public     { public declarations }     ImageArray: Array [1..5] of TImage;   end; var   Form1: TForm1; implementation Code: [Select] procedure TForm1.Form1Create(Sender: TObject); var i: Integer; begin   for i := 1 to 5 do     begin     ImageArray[i] := TImage.Create(Self);     ImageArray[i].Parent := Self;     // Set any other properties etc.    end; end;