Not sure on what to make title please edit if needed. I have a procedure
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(TShape(sender).Name);
end;
so i call the procedure on a Shape1MouseEnter. So i would think (Self) would send shape1 but it dont it sends the form(TFZone1Mod7) How i can get it to send the shape?
here is how i am calling it.
procedure TFZone1Mod7.Shape1MouseEnter(Sender: TObject);
begin
changetext(self);
end;
Inside this method
Selfis an object of typeTFZone1Mod7. And that’s your form. Remember thatSelfrefers to the instance associated with the active method. And in your code, the class is a form and so the instance,Self, is always a form instance.To know what
Selfis, look at the type that follows theprocedureorfunctionkeyword. TheSelfinstance is an instance of that type.In your situation, if you want to pass the shape you can pass either
Shape1, or to be more general,Sender. The latter allows you to share one event handler between multiple shapes.This sort of mistake highlights why you should use checked casts with the
asoperator. When you make a mistake, you will get informed of it immediately and in a helpful way. Your unchecked casts just lead to hard to understand cryptic errors.So I’d probably be inclined to declare
ChangeTextas receiving a parameter of typeTShape. And then calling it like this:And this allows you to remove the casts from
ChangeTextand confine them just to the event handlers which by necessity only have aTObjectinstance,Sender, available.