Click here to Skip to main content
15,569,319 members
Articles / Programming Languages / Visual Basic
Posted 16 Jul 2009


16 bookmarked

Easy Drag And Drop of Controls at Run-time

Rate me:
Please Sign up or sign in to vote.
4.60/5 (9 votes)
16 Jul 2009CPOL1 min read
Allow users to customize your forms


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.


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.
        For Each Control As Control In Me.Controls
            AddHandler Control.MouseDown, AddressOf startDrag
            AddHandler Control.MouseMove, AddressOf whileDragging
            AddHandler Control.MouseUp, AddressOf endDrag
        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
    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)
        End If
    End Sub
    Private Sub endDrag(ByVal sender As System.Object, ByVal e As System.EventArgs)
        dragging = False
        For Each Control As Control In Me.Controls
            My.Settings.controlLocations.Add(Control.Name & "!" _
		& Control.Location.X & "!" & Control.Location.Y)
    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


  • 16th July, 2009: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
United States United States
I started with programming business programs in VB 7 years ago, I focus on RAD (Rapid Application Development) and new technologies. After 500+ tools and programs I moved on to primarily focus on VB.NET and continue to create business applications for a couple Fortune 500 companies.

I have begun posting articles on my blog and step by step tutorials to this article can be found there.

Comments and Discussions

QuestionAWESOME Pin
Kudredin8-Apr-15 2:26
Kudredin8-Apr-15 2:26 
QuestionThank you Pin
Member 1152004816-Mar-15 3:00
Member 1152004816-Mar-15 3:00 
QuestionGood Job!!!! Pin
tiago.central6-May-14 9:21
tiago.central6-May-14 9:21 
GeneralThank You Pin
saqibsabir10-Apr-14 6:12
saqibsabir10-Apr-14 6:12 
QuestionThanking you Pin
Member 818469712-Mar-14 20:49
professionalMember 818469712-Mar-14 20:49 
QuestionTHANK YOU SIR! ^_^ Pin
christian_riosa3-Mar-14 20:28
christian_riosa3-Mar-14 20:28 
GeneralGood Job Pin
Patil Kishor15-Nov-13 5:45
Patil Kishor15-Nov-13 5:45 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey30-May-13 3:39
professionalManoj Kumar Choubey30-May-13 3:39 
QuestionGood work - Question though Pin
daveofgv21-Jan-13 11:17
daveofgv21-Jan-13 11:17 
QuestionCurious why? Pin
Middle Manager23-Jul-09 3:08
Middle Manager23-Jul-09 3:08 
AnswerRe: Curious why? Pin
John M Bundy23-Jul-09 9:12
John M Bundy23-Jul-09 9:12 
GeneralSugggestion Pin
Aneesur Rehman Khan17-Jul-09 3:42
Aneesur Rehman Khan17-Jul-09 3:42 
GeneralRe: Sugggestion Pin
John M Bundy17-Jul-09 4:45
John M Bundy17-Jul-09 4:45 
Generalnice Pin
Sergey Mirvoda16-Jul-09 20:03
Sergey Mirvoda16-Jul-09 20:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.