Drag and drop from one form to anywhere in another form
delphi, delphi-xe2, drag, drag-and-drop
Solution
Set a custom DragObject in the `OnStartDrag` event of the StringGrid:
{ TMyDragObject }
type
TMyDragObject = class(TDragControlObjectEx)
protected
DragText: String;
procedure Finished(Target: TObject; X, Y: Integer; Accepted: Boolean); override;
function GetDragCursor(Accepted: Boolean; X, Y: Integer): TCursor; override;
end;
procedure TMyDragObject.Finished(Target: TObject; X, Y: Integer; Accepted: Boolean);
begin
if Target is TCustomEdit then
TCustomEdit(Target).SelText := DragText;
inherited;
end;
function TMyDragObject.GetDragCursor(Accepted: Boolean; X, Y: Integer): TCursor;
begin
if TObject(DragTarget) is TCustomEdit then
Result := crDrag
else
Result := inherited GetDragCursor(Accepted, X, Y);
end;
{ TForm1 }
procedure TForm1.StringGrid1StartDrag(Sender: TObject; var DragObject: TDragObject);
begin
DragObject := TMyDragObject.Create(StringGrid1);
TMyDragObject(DragObject).DragText := StringGrid1.Cells[StringGrid1.Col,
StringGrid1.Row];
end;
Problem
I have an MDI form application with two Forms. The first one has a StringGrid, the second form has a lot of Edit controls and a few buttons. I need to drag from the grid and drop on the second Form and update one Edit with the value of the selected cell in the StringGrid in the first Form. This is pretty easy to do if I know in advance on which control the user will release the mouse left button as I can set the OnDragDrop event of that specific control. But I do not want to code multiple OnDragDrop events for each and every control on the second Form. How to intercept a form-wide message to intercept the drop operation on ANY control?