I have 12 shapes
Shape1
Shape2
.....
Shape12
and I have 12 labels
Label13
Label14
......
Label24
I was wondering if there is a way to write such a function, that on mouse enter on a shape would assign corresponding label to a different label, for example Label25:
Label25 :=
OnMouseEnter
shape1 -> label13
shape2 -> label14
...
shape12 -> label24
So if the mouse entered Shape1, Label25 would become equal to Label13, and if mouse entered Shape2, Label25 would become equal to Label14 and continue till if mouse entered Shape12 Label25 would become equal to Label24.
I know I can write
label25 := labelxx
on each of the mouse enter events. But thought there could be an easier way as the name of the label and shape correspond, where label # is 12 more then the shape # everytime.
After adding the suggested, I added this
procedure TFZone1Mod7.ChangeText(sender: TObject);
var
ShapeOrderNo: integer;
FoundComponent: TComponent;
begin
if TryStrToInt(copy(TShape(Sender).Name,6,MaxInt),ShapeOrderNo) then
begin
FoundComponent := FindComponent('label'+inttostr(ShapeOrderNo+12));
if (FoundComponent is TLabel) then
Label25.Caption := TLabel(FoundComponent).Caption
else
showmessage('not found');
end;
showmessage('failed try');
end;
procedure TFZone1Mod7.Shape1MouseEnter(Sender: TObject);
begin
changetext(self);
end;
end.
but everytime its ran i get failed try.
Am i sending the info wrong?
I don’t like this kind of a design, but well, you can use the common event handler for all your shapes and use the
FindComponentfunction to find a component by name there. You might then write comething like this (please note that it’s untested, written just in browser):