-2

Is there a way to change the property of a TImage which is stored in a variable?

I have a function that writes the Name propery of a TImage in the FigureSelectedName variable, and it writes the field Name in the FieldSelected variable.

Now my problem is:

FieldSelectedName.top
FieldSelectedName.left

This gives a error in Delphi (Illegal qualifier)

function moveFigure(FigName:String; FieldName:String):boolean;
var
  x:Integer;
  y:Integer;
begin
  if (FigureSelected=true) and (FieldSelected=true) then
   begin
     x := strtoint(FieldSelectedName[2]);
     y := Ord(FieldSelectedName[1])-64;

     FigureSelectedName.top  := 80 + (x * 70);
     FigureSelectedName.left := 80 + (y * 70);
   end;
end;
fantaghirocco
  • 4,761
  • 6
  • 38
  • 48
KS_HTK
  • 11
  • 1
  • 6
  • `FieldSelected` in your code is a `Boolean` so it can't have any `top` or whatsoever other property – fantaghirocco Dec 03 '15 at 20:41
  • None of this makes a lot of sense. I guess you have a string and want to find a variable of that name. You need to know which scope you want to search? Where do you intend to look for this variable? – David Heffernan Dec 03 '15 at 20:43
  • @fantaghirocco Sorry I meant FieldSelectedName wich contains the name of a TImage... And I want to move the Image to some Coordinates. I am not searching for a Variable, I have a Variable containing the Name of a TImage and want to move the TImage to some Coordinates. But this function shall be used for different TImages thats why I want to replace the TImage Name with a Variable. – KS_HTK Dec 03 '15 at 20:49
  • 2
    Where do you intend to look for the variable? In which scope? Why are you programming using names? Usually people do that by mistake. – David Heffernan Dec 03 '15 at 21:05
  • 2
    Your entire approach is wrong. If you need to change properties of an object inside a procedure, pass the object in as a parameter to the procedure. Trying to change it by looking it up by name is simply wrong, it adds unnecessary overhead, it leads to hard-to-debug problems, and it makes the code impossible to maintain,especially for other programmers who inherit your code in the future. Design it properly in the first place, instead of trying to solve the wrong problem afterward. – Ken White Dec 03 '15 at 23:05
  • FigureSelectedName contains a Name of an TImage Object wich is has been selected, I used this approach since I have it defined as a gloabal Variable because i use it for multiple Purposes... – KS_HTK Dec 03 '15 at 23:23
  • @KS_HTK - If you MUST use a Global variable (bad idea), make it of TImage, or TObject, not string - the NAME is irrelevant, the reference (address) is. – Gerry Coll Dec 03 '15 at 23:41
  • @Gerry Figured that out now, got it to work. Thanks! – KS_HTK Dec 04 '15 at 00:13

2 Answers2

0

Changing the system from using the TImage Names in the variable to having a TImage pointer in the variable did the trick.

FigureSelectedImage: TImage;
FieldSelectedImage:  TImage;

Then using the variable and the normal identifiers works fine.

FigureSelectedImage.Top := ...
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
KS_HTK
  • 11
  • 1
  • 6
-1

System.Classes.TComponent.FindComponent can be used to find a component in a form knowing its name.

  • check if what was found is really a TImage

  • use the TImage properties and methods


procedure TForm1.Button1Click(Sender: TObject);
var
  comp: TComponent;
  img: TImage;
begin
  comp := FindComponent('Image1');
  if comp is TImage then begin
    img := TImage(comp);
    img.Left := 0;
    img.Top := 0;
  end;
end;

Your method should be written like this, for the FindComponent method to work.

function moveFigure(FigName:String; FieldName:String): Boolean;
var
  x:Integer;
  y:Integer;
  comp: TComponent;
  img: TImage;
begin
  //Result := False;
  if FigureSelected and FieldSelected then
  begin
    x := strtoint(FieldSelectedName[2]);
    y := Ord(FieldSelectedName[1])-64;

    comp := Form1.FindComponent(FieldSelectedName);
    if comp is TImage then begin
      img := TImage(comp);
      img.Left := 80 + (y * 70);
      img.Top := 80 + (x * 70);
      //Result := True;
    end;
  end;
end;

But the method has many issues:

  • the method's arguments FigName:String and FieldName:String are never used in the method body

  • are you sure the coordinates you're looking for in the image's name will be ever only one digit long?

  • Ord(FieldSelectedName[1]): an ordinal of a Char looks like a strange value for the Y-axis

  • the method is declared returning Boolean but doesn't provide a value for the Result

fantaghirocco
  • 4,761
  • 6
  • 38
  • 48