Move certain controls at runtime

Posted on 2005-04-29
Last Modified: 2011-10-03
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.
Question by:mdlittle
    LVL 5

    Expert Comment

    The following component allows you to move controls at runtime:

    Author Comment

    Thanks, unfortunately these do not work with Delphi 2005. I should have specified my environment.
    LVL 22

    Accepted Solution

    try this article, it worked fine with Delphi 2005 for win32
    LVL 13

    Expert Comment

    do you mean in Delphi 2005, in the .NET part?

    Author Comment

    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.

    LVL 3

    Assisted Solution

    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... =)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
    In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now