I’ve created a label override that will allow my new control to be moved around the screen easily.
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.
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.
Screenshot:

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);
}
}
The OnMouseMove() code is bad. Do it like this instead:
The screen shot also shows evidence of the form not redrawing properly. You didn’t leave any clue as to what might cause that.