Drag a control on or off a panel with vb.net

Good morning. Well, last night was the first night I had gone without my melatonin in a while and it sucked! I am a person who has trouble sleeping as it is (my mind is always going 100 mph it seems) and melatonin was like a life saver. Well I ran out and be sure of this I will have some tonight!

Anyway I have an application I am working on that I needed to be able to drag a control (specifically a button) to a panel  or off a panel. Actually what happened was I needed to move the control around during testing as I was having one panel visible and then at other times it was not visible but I still needed access to that button. So this little routine I came up with did the trick. Nothing complicated but it might save you some time. I eliminated anything that did not have to do with this concept. Anyway, make it a great day!

Public Class Form1

    Dim dragging As Boolean

    Private Sub btnMove_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnMove.MouseDown       
        dragging = True
    End Sub

    Private Sub btnMove_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnMove.MouseMove       
        If dragging Then
            ‘new location = old location + change in mouse position
            btnMove.Location = btnMove.Location + e.Location
            btnMove.BringToFront()
            Me.Invalidate()
        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           
            dragging = False
        End If       
    End Sub

End Class

 

Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: