Click here to Skip to main content
11,790,021 members (43,779 online)
Click here to Skip to main content

Easy Drag And Drop of Controls at Run-time

, 16 Jul 2009 CPOL 22.7K 1.9K 13
Rate this:
Please Sign up or sign in to vote.
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. Smile | :)

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)


About the Author

John M Bundy
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.

You may also be interested in...

Comments and Discussions

QuestionAWESOME Pin
Kudredin8-Apr-15 1:26
memberKudredin8-Apr-15 1:26 
QuestionThank you Pin
Member 1152004816-Mar-15 2:00
memberMember 1152004816-Mar-15 2:00 
QuestionGood Job!!!! Pin
tiago.central6-May-14 8:21
membertiago.central6-May-14 8:21 
GeneralThank You Pin
saqibsabir10-Apr-14 5:12
membersaqibsabir10-Apr-14 5:12 
QuestionThanking you Pin
Member 818469712-Mar-14 19:49
memberMember 818469712-Mar-14 19:49 
QuestionTHANK YOU SIR! ^_^ Pin
christian_riosa3-Mar-14 19:28
memberchristian_riosa3-Mar-14 19:28 
GeneralGood Job Pin
Patil Kishor15-Nov-13 4:45
memberPatil Kishor15-Nov-13 4:45 
GeneralMy vote of 5 Pin
manoj kumar choubey30-May-13 2:39
membermanoj kumar choubey30-May-13 2:39 
QuestionGood work - Question though Pin
daveofgv21-Jan-13 10:17
memberdaveofgv21-Jan-13 10:17 
QuestionCurious why? Pin
Ed Bouras23-Jul-09 2:08
memberEd Bouras23-Jul-09 2:08 
AnswerRe: Curious why? Pin
John M Bundy23-Jul-09 8:12
memberJohn M Bundy23-Jul-09 8:12 
GeneralSugggestion Pin
Aneesur Rehman Khan17-Jul-09 2:42
memberAneesur Rehman Khan17-Jul-09 2:42 
GeneralRe: Sugggestion Pin
Jmbundy117-Jul-09 3:45
memberJmbundy117-Jul-09 3:45 
Generalnice Pin
waxa16-Jul-09 19:03
memberwaxa16-Jul-09 19:03 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.1509028.1 | Last Updated 16 Jul 2009
Article Copyright 2009 by John M Bundy
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid