Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Easy Drag And Drop of Controls at Run-time

0.00/5 (No votes)
16 Jul 2009 2  
Allow users to customize your forms
CPdragNdrop_small.GIF

Introduction

I have found a number of articles that show how to allow run-time drag and drop of controls, but most of them are either way more complicated than they have to be or way way harder than it has to be.

Disclaimer

This is my first article on CodeProject, be gentle! As is, this is NOT the best and most secure way to do this, but it is (I believe) the easiest. I have used this method in several programs and have had no issues whatsoever, but I'm sure you guys can find some. :)

What It Is and Isn't

The sample contains a very simple demo showing what the most basic results are, and step by step instructions can be found in a link in my sig. The code is simple, and I believe the article should be as well, so I will post the entire code in one chunk.

 Public Class Form1
    Dim dragging As Boolean
    Dim startX As Integer
    Dim startY As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, _
		ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 
        ''NorthwindDataSet.Employees' table. You can move, or remove it, as needed.
        Me.EmployeesTableAdapter.Fill(Me.NorthwindDataSet.Employees)
        For Each Control As Control In Me.Controls
            AddHandler Control.MouseDown, AddressOf startDrag
            AddHandler Control.MouseMove, AddressOf whileDragging
            AddHandler Control.MouseUp, AddressOf endDrag
        Next
        For Each Control As Control In Me.Controls
            For Each item In My.Settings.controlLocations
                If Split(item, "!")(0) = Control.Name Then
                    Control.Location = New Point(Split(item, "!")(1), _
						Split(item, "!")(2))
                End If
            Next
        Next
    End Sub
    Private Sub startDrag(ByVal sender As Object, _
		ByVal e As System.Windows.Forms.MouseEventArgs)
        dragging = True
        startX = e.X
        startY = e.Y
    End Sub
    Private Sub whileDragging(ByVal sender As System.Object, _
		ByVal e As System.Windows.Forms.MouseEventArgs)
        If dragging = True Then
            sender.Location = New Point(sender.Location.X + _
		e.X - startX, sender.Location.Y + e.Y - startY)
            Me.Refresh()
        End If
    End Sub
    Private Sub endDrag(ByVal sender As System.Object, ByVal e As System.EventArgs)
        dragging = False
        My.Settings.controlLocations.Clear()
        For Each Control As Control In Me.Controls
            My.Settings.controlLocations.Add(Control.Name & "!" _
		& Control.Location.X & "!" & Control.Location.Y)
        Next
        My.Settings.Save()
    End Sub
End Class

What the Code Does

The 3 subs do all the work, and there is a blank My.Settings named controlLocations that stores the locations, if not, the form will reset each time. Keeping with simplicity, I did not include some things you may want:

  • A button to turn the dragging ability on or off
  • A way to specify only certain types of controls for movement

If there is a request, I can add that.

Points of Interest

  • Quick and easy

History

  • 16th July, 2009: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here