Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows Forms
  4. Drag and move a button anywhere on the form

Drag and move a button anywhere on the form

Scheduled Pinned Locked Moved Windows Forms
learning
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    phani25485
    wrote on last edited by
    #1

    Hi I want to develop a small app where i can position some controls like button, textbox, label etc and when i run the form I must be able to move the controls ie drag and drop anywhere on the form could u refer me few articles with simple explanation, I am a beginner , thanks

    Y 1 Reply Last reply
    0
    • P phani25485

      Hi I want to develop a small app where i can position some controls like button, textbox, label etc and when i run the form I must be able to move the controls ie drag and drop anywhere on the form could u refer me few articles with simple explanation, I am a beginner , thanks

      Y Offline
      Y Offline
      yu jian
      wrote on last edited by
      #2

      Add the event OnMouseDown of controls. When OnMouseDown, get the current mouse position and set the new mouse position to the controls. When OnMouseUp, donnot set the new mouse positon to the controls.

      S 1 Reply Last reply
      0
      • Y yu jian

        Add the event OnMouseDown of controls. When OnMouseDown, get the current mouse position and set the new mouse position to the controls. When OnMouseUp, donnot set the new mouse positon to the controls.

        S Offline
        S Offline
        Som Shekhar
        wrote on last edited by
        #3

        A few changes from Yu-Jian. 1. create two variables a)bool allowDrag b)Point lastPosition 2. on Mouse Down, set allowDrag to true and set lastPosition of Mouse 3. on Mouse Up, set allowDrag to false 4. on Mouse Move, if allowDrag is true, change the location of the button based on the new location and previous location.

        P 1 Reply Last reply
        0
        • S Som Shekhar

          A few changes from Yu-Jian. 1. create two variables a)bool allowDrag b)Point lastPosition 2. on Mouse Down, set allowDrag to true and set lastPosition of Mouse 3. on Mouse Up, set allowDrag to false 4. on Mouse Move, if allowDrag is true, change the location of the button based on the new location and previous location.

          P Offline
          P Offline
          phani25485
          wrote on last edited by
          #4

          Could u please provide the code, its confusing and i am new to windows programming

          Y 1 Reply Last reply
          0
          • P phani25485

            Could u please provide the code, its confusing and i am new to windows programming

            Y Offline
            Y Offline
            yu jian
            wrote on last edited by
            #5

            Only one button in the form. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication5 { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.button1.MouseMove += new MouseEventHandler(Form1_MouseMove); } private void button1_MouseDown(object sender, MouseEventArgs e) { this._allowDrag = true; } /// <summary> /// Recore the bool value; if true, the button's positon will be changed; /// </summary> private bool _allowDrag; private void Form1_MouseMove(object sender, MouseEventArgs e) { if (this._allowDrag) { this.button1.Location = new Point(e.X + this.button1.Location.X, e.Y + this.button1.Location.Y); } } private void button1_MouseUp(object sender, MouseEventArgs e) { if (this._allowDrag) this._allowDrag = false; } } }

            P M 2 Replies Last reply
            0
            • Y yu jian

              Only one button in the form. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication5 { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.button1.MouseMove += new MouseEventHandler(Form1_MouseMove); } private void button1_MouseDown(object sender, MouseEventArgs e) { this._allowDrag = true; } /// <summary> /// Recore the bool value; if true, the button's positon will be changed; /// </summary> private bool _allowDrag; private void Form1_MouseMove(object sender, MouseEventArgs e) { if (this._allowDrag) { this.button1.Location = new Point(e.X + this.button1.Location.X, e.Y + this.button1.Location.Y); } } private void button1_MouseUp(object sender, MouseEventArgs e) { if (this._allowDrag) this._allowDrag = false; } } }

              P Offline
              P Offline
              phani25485
              wrote on last edited by
              #6

              Thanks very much it worked

              D 1 Reply Last reply
              0
              • P phani25485

                Thanks very much it worked

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                Instead of cluttering up your form code, take a look at my article on Create your Own Runtime Movable Windows Forms Controls[^]. This will make your button much easier to code with and you don't have to worry about wiring up your events for every normal button you drop on the form. You also don't have to worry about if you're dropping the button on the form or in a panel or in any other container control.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                     2006, 2007, 2008
                But no longer in 2009...

                1 Reply Last reply
                0
                • Y yu jian

                  Only one button in the form. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication5 { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.button1.MouseMove += new MouseEventHandler(Form1_MouseMove); } private void button1_MouseDown(object sender, MouseEventArgs e) { this._allowDrag = true; } /// <summary> /// Recore the bool value; if true, the button's positon will be changed; /// </summary> private bool _allowDrag; private void Form1_MouseMove(object sender, MouseEventArgs e) { if (this._allowDrag) { this.button1.Location = new Point(e.X + this.button1.Location.X, e.Y + this.button1.Location.Y); } } private void button1_MouseUp(object sender, MouseEventArgs e) { if (this._allowDrag) this._allowDrag = false; } } }

                  M Offline
                  M Offline
                  Mycroft Holmes
                  wrote on last edited by
                  #8

                  I wonder why someone univoted you for the example code.

                  Never underestimate the power of human stupidity RAH

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups