move image by name

Greetings experts,

In my program, i have an image, 2 edit boxes and a button. In the edit boxes, i input an X value, and a Y value. Upon clicking the button, the image will slowly move from its current position, to the new position, as determined by the X and Y values entered into my edit boxes.

My function to move the image works fine, however i now wish to use this function to be able to move different images. Is it possible to tell my function which image to move, by entering the name of the image into an edit box? For example, lets say i have 2 images on my form, named image1 and image2 respectively. I now enter the X and Y values into my edit boxes, and into a third edit box, i enter 'image2'. When i click the button, i need the function to move the image named 'image2' to the new coordinates. If i were to enter 'image1' into the third edit box, the function would have to move the image named 'image1' to the new coordinates.

I am looking for a solution, so that i can remove all bits of code from my function that specifically refers to an image. For example, i cannot use image1.left:=image1.left+3, as the function may be used to move image1 or image2.

To make it clear what i am looking for, i have written very brief psuedo code below.

x:=edit3.text   (where x refers to an image)

so in my pseudo code, if edit3.text was 'image2' then the second line of my pseudo code would in effect read image2.left:=image2.left+3.

Is this at all possible to achieve?
Any help appreciated.
Tried to make this question as clear as i can. please say if you dont fully understand.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

here something like this:

function TForm1.GetImage(Name: String): TImage;
  i: integer;
  for i:= 0 to ComponentCount - 1 do
    if ((Components[i] is TImage) and (Components[i].Name = Name))
      then Result := TImage(Components[i]);


  image: TImage;


  image := GetImage(edit3.text);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GreenandroidAuthor Commented:
Well the solution you posted you be absolutely PERFECT for my needs, if only i could stop it from spitting out errors.
It really doesnt like the function you have declared, stating that Timage is an undeclared identifier etc.
How could i stop this. Did you encounter the same problem?
TImage is declared in ExtCtrls

so just include it in your uses clause
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

If you have images on your form, then the component you're using should be defined already. What type of component are your images? Change TImage to that component.
GreenandroidAuthor Commented:
My silly mistake....
Thanks BlacktigerX, and a big thanks to ezraa!
Solution worked perfectly.

could it not be shortened to

function TForm1.GetImage(AName: String): TImage;
  result := findcomponent(AName) as TImage;


meikl ;-)
I guess it could meikl,

some of us are still learning the shortcuts -  :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.