• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

Move certain controls at runtime

I would like to move certain controls at runtime (buttons, imagepanel) with the mouse. I would also like them to be layed out in a grid after moving. I hope this is clear. Please supply some sample code. Thanks a bunch.
2 Solutions
The following component allows you to move controls at runtime:

mdlittleAuthor Commented:
Thanks, unfortunately these do not work with Delphi 2005. I should have specified my environment.
Mohammed NasmanSoftware DeveloperCommented:
try this article, it worked fine with Delphi 2005 for win32
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

do you mean in Delphi 2005, in the .NET part?
mdlittleAuthor Commented:
Delphi 2005 in the VCL mode. I also tried the above example from mnasman and it is not really a good example. The form drags, all controls on message boxes drag, etc. it is not very clean.

Here is a simple enough method to move a button at runtime (hold ctrl+shift to drag):

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
     if Shift = [ssCtrl,ssShift] then
          button1.Left := x;
          button1.Top := y;

If you want to have it snap to a grid of 20x20:

          button1.Left := (round(x/20)*20);
          button1.Top := (round(y/20)*20);

Math to the snap:
eg. mouse is at 45x67
45 / 20 = 2.25, rounded down to 2,  2x20 = 40
67 / 20 = 3.35, rounded down to 3,  3x20 = 60

TShiftState is a set of values:
ssShift      The Shift key is held down.
ssAlt      The Alt key is held down.
ssCtrl      The Ctrl key is held down.
ssLeft      The left mouse button is held down.
ssRight      The right mouse button is held down.
ssMiddle      The middle mouse button is held down.
ssDouble      The mouse was double-clicked.

Hope this helps... =)

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now