Click here to Skip to main content
Click here to Skip to main content

Easy Drag And Drop of Controls at Run-time

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

Share

About the Author

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

 
QuestionGood Job!!!! Pinmembertiago.central6-May-14 9:21 
GeneralThank You Pinmembersaqibsabir10-Apr-14 6:12 
QuestionThanking you PinmemberMember 818469712-Mar-14 20:49 
QuestionTHANK YOU SIR! ^_^ Pinmemberchristian_riosa3-Mar-14 20:28 
GeneralGood Job PinmemberPatil Kishor15-Nov-13 5:45 
GeneralMy vote of 5 Pinmembermanoj kumar choubey30-May-13 3:39 
QuestionGood work - Question though Pinmemberdaveofgv21-Jan-13 11:17 
QuestionCurious why? PinmemberEd Bouras23-Jul-09 3:08 
AnswerRe: Curious why? PinmemberJohn M Bundy23-Jul-09 9:12 
GeneralSugggestion PinmemberAneesur Rehman Khan17-Jul-09 3:42 
GeneralRe: Sugggestion PinmemberJmbundy117-Jul-09 4:45 
Generalnice Pinmemberwaxa16-Jul-09 20: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
Web04 | 2.8.141220.1 | Last Updated 16 Jul 2009
Article Copyright 2009 by John M Bundy
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid