I found a good sample Here, but I have a few problems with it. 1. It is not placing the control to the correct position where the mouse left off becase the control is a big one.
- It could be pushed out of the screen.. I want it should stay within the screen boundaries.
 
This is my code:
   Public Sub Form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyControl.MouseMove
    If Not _capturingMoves Then
        Return
    End If
    X = e.X
    Y = e.Y
End Sub
Public Sub Form1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyControl.MouseUp
    If _capturingMoves Then
        ' Do any final placement
        MyControl.Location = New Point(X, Y)
        _capturingMoves = False
    End If
End Sub
				
                        
I wrote something like this for dragging divs on a webpage...
The general approach was to save the coordinates on mousedown, get the coordinates on mouseup, and shift the object's location by the difference.
Here's some example code:
I made a
DragInfoclass that keeps the initial mouse coords and initial location. I then store one of these guys in the control'sTagon the mousedown event:My test control is just a panel that I put from the toolbox. It could be anything I guess. Here are my mousedown, mousemove, and mouseup event handlers for the panel (
Panel1):There you go! That works. Note that the mousemove method checks if the control is within the form's clientrectangle.
Or, a more generic way to go:
Now you can just use
MakeDraggable(Panel1)or any other control to make it draggable!Edit: Both examples now keep the control from being dragged out of bounds.