How To Move ANY Visible Component at Runtime

This code will move any control at runtime, even non TWinControl descendants.

Make sure ExtCtrls is in your USES clause.
Then set the OnMouseDown event of your controls to the following code.

procedure TForm1.MoveControl(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
  TempPanel : TPanel;
  Control : TControl;
  //Release the MOUSEDOWN status
  if Sender is TWinControl then
    //Component has a Handle, move it directly
    //Component has no handle, move it in a TPanel
      Control := TControl(Sender);
      TempPanel := TPanel.Create(Self);
      with TempPanel do
     //Replace component with TempPanel
        Caption := '';
        BevelOuter := bvNone;

        Parent := Control.Parent;

        //Put our control in the TempPanel
        Control.Parent := TempPanel;

        //Move TempPanel with the control inside it

        //Put the component where the panel was dropped
      Control.Parent := Parent;
        Control.Left := Left;
        Control.Top := Top;


Share this article!

Follow us!

Find more helpful articles: