C# - Moving a control to the mouse's position

Posted by APShredder on Stack Overflow See other posts from Stack Overflow or by APShredder
Published on 2010-04-24T03:41:46Z Indexed on 2010/04/24 3:43 UTC
Read the original article Hit count: 254

Filed under:
|
|
|
|

Hello everybody. I am trying to get a control to follow the cursor when the user clicks and drag the control. The problem is that 1.) the control doesn't go to the mouse's position, and 2.) the control flickers and flies all over the place. I've tried a few different methods of doing this, but all so far have failed.

I've tried:

protected override void OnMouseDown(MouseEventArgs e)
{
     while (e.Button == System.Windows.Forms.MouseButtons.Left)
     {
          this.Location = e.Location;
     }
}

and

protected override void OnMouseMove(MouseEventArgs e)
{
     while (e.Button == System.Windows.Forms.MouseButtons.Left)
     {
          this.Location = e.Location;
      }
}

but neither of these work. Any help is appreciated, and thanks in advance!

© Stack Overflow or respective owner

Related posts about c#

Related posts about mouse