title: Drag and Drop - Moving a Label in Winforms
body: <p>I've created a label override that will allow my new control to be moved around the screen easily.</p>
  
  <p>I've attached the code below, but when I run the application or attempt to move the label it's always off. It also will sometimes just completely vanish, leave a trail or reset to the 0,0 location. Have a look at the screenshot.</p>
  
  <p>I used to have this working 100%, but after some recent tweaking it has gone to the dogs again and I'm not sure how to get it working.</p>
  
  <p><strong>Screenshot:</strong>
  <img src="https://i.stack.imgur.com/LGsv3.png" alt="Attempting to Drag"></p>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>internal sealed class DraggableLabel : Label
  {
  
      private bool _dragging;
      private int _mouseX, _mouseY;
  
      public DraggableLabel()
      {
          DoubleBuffered = true;
      }
  
      protected override void OnMouseMove(MouseEventArgs e)
      {
        if (_dragging)
          {
              Point mposition = PointToClient(MousePosition);
              mposition.Offset(_mouseX, _mouseY);
  
              Location = mposition;
          }
          base.OnMouseMove(e);
      }
  
      protected override void OnMouseDown(MouseEventArgs e)
      {
         if (e.Button == MouseButtons.Left)
          {
             _dragging = true;
             _mouseX = -e.X;
             _mouseY = -e.Y;
             BringToFront();
             Invalidate();
          }
          base.OnMouseDown(e);
      }
  
      protected override void OnMouseUp(MouseEventArgs e)
      {
          if (_dragging)
          {
              _dragging = false;
              Cursor.Clip = new Rectangle();
              Invalidate();
          }
  
          base.OnMouseUp(e);
      }
  
  }
  </code></pre>