Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable defining variable

Status
Not open for further replies.

cheesemunger

Programmer
Jul 6, 2007
11
OK, I have got a form with 8 images named image1,image2,image3.......,image8
I want to have a piece of code in a timer procedure,running every 0.1 of a second, that move each of the images to the right in turn.

Could you have this code?

var
number:integer;

imagenumber.left:=imagenumber.left+1
if number=8 then number:=1
else
number:=number+1

if this is not the code then can you tell me what is?
 
no that is not possibile.

one solution is to use TObjectList for this type of tasks

Code:
....
uses Contnrs,SysUtils,...

....

procedure SetupImages;
begin
 ObjectList.add(Image1);
 ObjectList.add(Image2);
 ...
 ObjectList.add(Image8);
end;

procedure MoveImages;

var Index : Integer;
    Image : TImage; 

begin
 for Index := 0 to ObjectList.Count - 1 do
  begin
   Image := TImage(ObjectList[Index]);
   Image.Left := Image.Left + 1;
  end;
end;

....
var ObjectList : TObjectList;

begin
 ObjectList := TObjectList.Create(False);
 try
  SetupImages;
  MoveImages; 
 finally
  FreeAndNil(ObjectList);
 end;
end;

I use this approach mainly with dynamicly created objects.
if you are manipulating form objects, you can use the CotrolByName and ComponentByName functions to find the objects you want.

/Daddy


-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Ok I understand a little of your code but not much, I am moving object which have already been placed on a form.. how do i do this?
 
Yes, it is possible!

Assuming your images are called Image1, Image2, Image3 ...
Code:
procedure TForm1.Timer1Timer(Sender: TObject);
var
  number: integer;
  imagename: string;
  image: TImage;
begin
  for number := 1 to 8 do begin
    imagename := 'Image' + IntToStr(number);
    image := FindComponent( imageName ) as TImage;
    image.Left := image.Left + 1;
    Application.ProcessMessages;
  end;
end;


Andrew
Hampshire, UK
 
you can also put them in an array.
Code:
var images: array [0..7] of timage;
 a: integer;
begin
images[0]:=image1;
images[1]:=image2;
images[2]:=image3;
images[3]:=image4;
images[4]:=image5;
images[5]:=image6;
images[6]:=image7;
images[7]:=image8;

for a := 0 to 7 do
 begin
  images[a].Left := images[a].Left + 1;
 end;
end;

Aaron
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top