Talk:ControlsTDragState (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Faulty typecasts Source as TForm1. Source is TLable. Need modify: TForm1(Source).Left := X; TForm1(Source).Top := Y; to TLabel(Source).Left := X; TLabel(Source).Top := Y;


Thank you for your input! We will fill in a task with the modifications you suggested. -- Denisa Tibeica



I have updated the topic. The typecast is not faulty but is essentially "wrong" because one cannot see that TLabel and TForm1 (or any-other form) descend from TWinControl. TWinControl on the other side derive directly from TControl which exposes: Left and Top. That is why it works with any-other typecast from a control that actually derives from TWinControl.

In this particular example, Left and Top belong to both TForm1 and TLabel, because both use them from TControl.

This example would have worked with the following typecast too:

procedure TForm1.FormDragDrop(Sender, Source: TObject; X, Y: Integer);
begin
  TControl(Source).Left := X;
  TControl(Source).Top := Y;
end;

But I agree, more clearly is to use a TLabel(Source) typecast.

--AG