Drag and Drop in Windows Forms - A Primer

Dragging a Control around

I wondered about this one purely because of the design side of VB. i.e. dropping controls onto a form and sizing them or moving them when you are creating a form in VS.Net for example. This is pretty nifty but try as I might I could not find an example or any help at all on how to do this until I visited www.planetsourcecode.com. I found some code that did something similar to what I wanted but more importantly it answered the only problem I had with my own implementation of the idea. To explain I had decided the only way to do this was to go back to the basic idea I mentioned at the beginning of this article, that is the idea of using a boolean to indicate when the user is dragging and applying this to the mousedown, mousemove and mouseup events. This is because the control has to move with the mouse not show an effect as in normal D&D. I had gotten to the point where I could move the control around a form but it was not smooth movement. I was having problems calculating the difference in mouseposition every time the mousemove event fired. The solution was the Offset method of a Point. So here's the code. I'm using a button as an example. I'll endeavour to explain it in due course:-

Private Sub btnMove_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnMove.MouseDown
    If e.Button = MouseButtons.Left Then
        Dragging = True
        mousex = -e.X
        mousey = -e.Y
        Dim clipleft As Integer = Me.PointToClient(MousePosition).X - btnMove.Location.X
        Dim cliptop As Integer = Me.PointToClient(MousePosition).Y - btnMove.Location.Y
        Dim clipwidth As Integer = Me.ClientSize.Width - (btnMove.Width - clipleft)
        Dim clipheight As Integer = Me.ClientSize.Height - (btnMove.Height - cliptop)
        Cursor.Clip = Me.RectangleToScreen(New Rectangle(clipleft, cliptop, clipwidth, clipheight))
        btnMove.Invalidate()
    End If
End Sub

Private Sub btnMove_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnMove.MouseMove
    If Dragging Then
         'move control to new position
        Dim MPosition As New Point()
        MPosition = Me.PointToClient(MousePosition)
        MPosition.Offset(mousex, mousey)
        'ensure control cannot leave container
        btnMove.Location = MPosition
    End If
End Sub

Private Sub btnMove_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnMove.MouseUp
     If Dragging Then
        'end the dragging
        Dragging = False
        Cursor.Clip = Nothing
        btnMove.Invalidate()
    End If
End Sub

You might also like...

Comments

About the author

Brian O'Connell Ireland

Microsoft Certified Applications Developer with 10 years experience developing web based applications using asp, asp.net for a Local Authority in Dublin. Clings to a firm belief that a web appli...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Debuggers don't remove bugs. They only show them in slow motion.”