Recent

Author Topic: Loop Through Components  (Read 8652 times)

rjmatm

  • New Member
  • *
  • Posts: 11
Loop Through Components
« on: June 09, 2005, 08:36:35 pm »
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

  • Hero Member
  • *****
  • Posts: 537
Loop Through Components
« Reply #1 on: June 10, 2005, 05:04:25 pm »
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;
What's in a sig? Would my posting look less if it didnot have a sig? (Free after William S.) :)

:( Why cannot I upload my own Avatar? :(

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2673
Loop Through Components
« Reply #2 on: June 13, 2005, 02:03:24 pm »
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
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

rjmatm

  • New Member
  • *
  • Posts: 11
Loop Through Components
« Reply #3 on: June 13, 2005, 07:07:05 pm »
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

  • Hero Member
  • *****
  • Posts: 537
Loop Through Components
« Reply #4 on: June 13, 2005, 07:17:17 pm »
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;
What's in a sig? Would my posting look less if it didnot have a sig? (Free after William S.) :)

:( Why cannot I upload my own Avatar? :(

rjmatm

  • New Member
  • *
  • Posts: 11
Loop Through Components
« Reply #5 on: June 14, 2005, 04:22:03 am »
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;

 

TinyPortal © 2005-2018