Fabrizio
Fabrizio

Reputation: 8053

Why the OnEndDrag event is not called?

I'm trying to detect the end of a drag & drop operation by using the TControl.OnEndDrag event.

It seems that the OnEndDrag event is never called when the DragObject parameter is assigned from the OnStartDrag event.

  TMyForm = class(TForm)
    procedure FormCreate(Sender: TObject);
  public
    MyLabel : TLabel;
    procedure MyOnEndDrag(Sender, Target: TObject; X, Y: Integer);
    procedure MyOnStartDrag(Sender: TObject; var DragObject: TDragObject);
  end;

procedure TMyForm.FormCreate(Sender: TObject);
begin
  MyLabel := TLabel.Create(Self);
  MyLabel.Caption := 'Drag me';
  MyLabel.Left := 50;
  MyLabel.Top := 50;
  MyLabel.OnStartDrag := MyOnStartDrag;
  MyLabel.OnEndDrag := MyOnEndDrag;
  MyLabel.DragMode := dmAutomatic;
  MyLabel.Parent := Self;
end;

procedure  TMyForm.MyOnEndDrag(Sender, Target: TObject; X, Y: Integer);
begin
  ShowMessage('MyOnEndDrag');
end;

procedure  TMyForm.MyOnStartDrag(Sender: TObject; var DragObject: TDragObject);
begin
  DragObject := TDragObjectEx.Create;
end;

I think the problem could be related to the DragObject's class but I don't understand what I'm doing wrong. How can I make sure the OnEndDrag event is called?

Upvotes: 2

Views: 560

Answers (1)

Sertac Akyuz
Sertac Akyuz

Reputation: 54822

A drag object needs to know which control it should notify when the drag ends. The specialized class for this kind of operation in the VCL, that is a drag object that can be associated with a single control, is a TBaseDragControlObject. TDragControlObject[Ex] is the appropriate descendant which deals with dropping as opposed to its sibling TDragDockObject which deals with docking.

DragObject := TDragControlObjectEx.Create(MyLabel);

Upvotes: 2

Related Questions