Click here to Skip to main content
15,885,048 members
Articles / Programming Languages / Visual Basic

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 63.1K   3.4K   16   15
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.

VB.NET
 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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Other
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.
http://jmbundy.blogspot.com/

Comments and Discussions

 
QuestionChild control not working Pin
Member 1476781316-Jan-24 2:53
Member 1476781316-Jan-24 2:53 
QuestionAWESOME Pin
Kudredin8-Apr-15 1:26
Kudredin8-Apr-15 1:26 
QuestionThank you Pin
Member 1152004816-Mar-15 2:00
Member 1152004816-Mar-15 2:00 
QuestionGood Job!!!! Pin
tiago.central6-May-14 8:21
tiago.central6-May-14 8:21 
GeneralThank You Pin
saqibsabir10-Apr-14 5:12
saqibsabir10-Apr-14 5:12 
QuestionThanking you Pin
Member 818469712-Mar-14 19:49
professionalMember 818469712-Mar-14 19:49 
QuestionTHANK YOU SIR! ^_^ Pin
christian_riosa3-Mar-14 19:28
christian_riosa3-Mar-14 19:28 
GeneralGood Job Pin
Patil Kishor15-Nov-13 4:45
Patil Kishor15-Nov-13 4:45 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey30-May-13 2:39
professionalManoj Kumar Choubey30-May-13 2:39 
QuestionGood work - Question though Pin
daveofgv21-Jan-13 10:17
daveofgv21-Jan-13 10:17 
How can I make it where a button click locks the controls from moving.... and a another button click for the user to start moving again?

It appears to be easy if the user accidentally drags a control.

Thanks
QuestionCurious why? Pin
Middle Manager23-Jul-09 2:08
Middle Manager23-Jul-09 2:08 
AnswerRe: Curious why? Pin
John M Bundy23-Jul-09 8:12
John M Bundy23-Jul-09 8:12 
GeneralSugggestion Pin
Aneesur Rehman Khan17-Jul-09 2:42
Aneesur Rehman Khan17-Jul-09 2:42 
GeneralRe: Sugggestion Pin
John M Bundy17-Jul-09 3:45
John M Bundy17-Jul-09 3:45 
Generalnice Pin
Sergey Mirvoda16-Jul-09 19:03
Sergey Mirvoda16-Jul-09 19: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.