Ask Your Question

Revision history [back]

Yes, the new position of a dynamically shifted control can be saved in Delphi.

You can use the OnMouseUp event of the control to save its new position. For example:

  1. Declare a variable in the form or unit to hold the position of the control:

var ControlPosition: TPoint;

  1. In the OnMouseDown event of the control, record its current position:

procedure TForm1.Button1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin ControlPosition := Point(Sender.Left, Sender.Top); end;

  1. In the OnMouseMove event of the control, move it based on the mouse movement:

procedure TForm1.Button1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); begin if ssLeft in Shift then begin Sender.Left := Sender.Left + X - ControlPosition.X; Sender.Top := Sender.Top + Y - ControlPosition.Y; end; end;

  1. In the OnMouseUp event of the control, save its new position to a file or database:

procedure TForm1.Button1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin // save new position to a file or database end;

Note that you will need to implement a way to load the saved position when the form is loaded. This can be done in the FormCreate event, for example.