Drag and move a button anywhere on the form
-
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
-
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
-
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.
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.
-
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.
Could u please provide the code, its confusing and i am new to windows programming
-
Could u please provide the code, its confusing and i am new to windows programming
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; } } }
-
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; } } }
Thanks very much it worked
-
Thanks very much it worked
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... -
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; } } }
I wonder why someone univoted you for the example code.
Never underestimate the power of human stupidity RAH