How to move a button ? RRS feed

  • Question

  • Hello.I have 2 question?
          1) How I can move a button (or picture) in the form.Example:  "The user moves a button from the bottom to the top of the form".

          2) If I have a form ( 800x600 ) how I can make an animation (with DirectX or something..) with the dimension: 90x30 (for example) in the center or in the bottom of the form
    Thursday, May 25, 2006 6:46 PM

All replies

  • And 3rd:
                  3) Why my application is runnig slowly.Is because a lot of bitmap images?
    Thursday, May 25, 2006 6:48 PM
  • Hi,
    1- to move your button, this is one way of doing it, add 3 event handlers for OnMouseDown, OnMouseMove, OnMouseUp, in mouse down and up you signal that you're starting the drag action and perform the moving in the mouse move event:

    // Indicates if we're dragging the button
    bool drag = false;
    // Indicates the offset of the mouse relative to the button's location when we start dragging
    Point mousePos;
    private void button2_MouseDown(object sender, MouseEventArgs e)
        drag = true;
        mousePos = new Point(Cursor.Position.X - button2.Location.X, Cursor.Position.Y - button2.Location.Y);

    private void button2_MouseMove(object sender, MouseEventArgs e)
            button2.Location = new Point(Cursor.Position.X - mousePos.X, Cursor.Position.Y - mousePos.Y);

    private void button2_MouseUp(object sender, MouseEventArgs e)
        drag = false;

    2- Create a panel where you want and with the size you want and use the panel's handle to create the DirectX device

    3- It could be. It depends on the code you're processing.. but lots of bitmaps can make the application run slower if you're always rendering them. Try to enable double buffering in your form:

    this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);

    Thursday, May 25, 2006 10:34 PM
  • While setting the styles will work, it is equivalent to set the DoubleBuffered property to true (new in 2.0). It also uses OptimizedDoubleBuffer.

    Actually, while double buffering will improve on the flickering, I doubt it improves performance.


    Friday, May 26, 2006 2:20 AM
  • How many DoEvents() statements do you have in your app?

    This will harm you if you have them in the wrong spots and in too many places.



    Friday, May 26, 2006 8:33 AM
  • You're right about double buffering, it won't improve performance. Don't know why I replied like I did but I guess it was because of a post in the DirectX thread related to flickering. Sorry about that.
    Friday, May 26, 2006 9:10 AM
  • n0n4m3 thanks for the code but:
                1 - How to make that button to move only on a determinate area
                2 - Is working slow
    • Proposed as answer by Crocus Saturday, August 13, 2011 12:25 PM
    Friday, May 26, 2006 11:45 AM
  • 1- One possible solution:

    // Just a sample rectangle where you can move
    Rectangle rect = new Rectangle(10, 10, 300, 300);
    Point newPosition = new Point();
    private void button2_MouseMove(object sender, MouseEventArgs e)
        if (!drag)

        newPosition.X = Cursor.Position.X - mousePos.X;
    newPosition.Y = Cursor.Position.Y - mousePos.Y;
        if (newPosition.X < rect.X)
            newPosition.X = rect.X;
    if (newPosition.X + button2.Size.Width > rect.X + rect.Width)
            newPosition.X = rect.X + rect.Width - button2.Size.Width;

        if (newPosition.Y < rect.Y)
            newPosition.Y = rect.Y;
    if (newPosition.Y + button2.Size.Height > rect.Y + rect.Height)
            newPosition.Y = rect.Y + rect.Height - button2.Size.Height;

        button2.Location = newPosition;

    2- What is slow? The code to move the button? Well, I don't see how that code can be such a problem in your program's performance.
    Friday, May 26, 2006 12:55 PM