Drag Drop Button

Is it possible to Drag and Drop a Button from a panel to another panel or to the form


Answer this question

Drag Drop Button

  • Sudheer Palyam

    Thanks, for posting this. That works great!

    Don't suppose you got an easy solution for stretching a button


  • fatmav

    You can do this with the normal D+D logic. Start a new Windows project, drop a button and a panel on the form, then paste this code:

    public partial class Form1 : Form {
    private Point mDropOffset;
    public Form1() {
    InitializeComponent();
    this.AllowDrop = true;
    this.panel1.AllowDrop = true;
    this.button1.MouseDown += GimmeAButtonStart;
    this.panel1.DragDrop += GimmeAButtonDrop;
    this.panel1.DragEnter += GimmeAButtonEnter;
    this.DragDrop += GimmeAButtonDrop;
    this.DragEnter += GimmeAButtonEnter;
    }
    private void GimmeAButtonStart(object sender, MouseEventArgs e) {
    // Start D+D if its a button and the control key is down
    Button btn = sender as Button;
    if (btn == null || Control.ModifierKeys != Keys.Control) return;
    mDropOffset = e.Location;
    btn.DoDragDrop(btn, DragDropEffects.Move);
    }
    private void GimmeAButtonEnter(object sender, DragEventArgs e) {
    // Show "Move" icon if user is D+D-ing a button
    if (e.Data.GetDataPresent(typeof(Button))) e.Effect = DragDropEffects.Move;
    }
    private void GimmeAButtonDrop(object sender, DragEventArgs e) {
    // Move the button
    Control ctl = (Control)sender;
    Button btn = (Button)(e.Data.GetData(typeof(Button)));
    if (btn == null) return;
    btn.Location = ctl.PointToClient(new Point(e.X - mDropOffset.X, e.Y - mDropOffset.Y));
    ctl.Controls.Add(btn);
    }
    }

    Build + run, hold down the Ctrl key and drop and drop the button around. Note that the Ctrl key is necessary to avoid interfering with the normal button operation. Anticipating your next request: no, you can't easily show your button moving when you need to drop the button from a form to a panel; you'll have Z-order problems.


  • Leandro Rodrigues

    Hi I just modify some code some other one and tried it seems can drag button between two forms,
    Best Regards!
    using
    System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    namespace WindowsApplication2
    {
    public partial class Form4 : Form
    {
    public Form4()
    {
    InitializeComponent();
    }
    private int OldX , OldY;
    private string Control_Name="";
    private void button1_MouseDown(object sender, MouseEventArgs e)
    {
    button1.Tag = button1.Bounds;
    OldX = e.X;
    OldY = e.Y;
    }
    private void button1_MouseUp(object sender, MouseEventArgs e)
    {
    if (button1.Bounds == (Rectangle)button1.Tag)
    MessageBox.Show("Move your 'click' code here");
    }
    private void button1_MouseMove(object sender, MouseEventArgs e)
    {

    if (e.Button == MouseButtons.Left)
    {
    button1.Left += e.X - OldX;
    button1.Top += e.Y - OldY;
    }
    if (this.button1.Left > this.panel1.Width)
    {
    //MessageBox.Show(e.X.ToString());
    this.panel1.Controls.Clear();
    this.panel2.Controls.Add(this.button1);
    this.button1.Location = new Point(e.X, e.Y);
    }
    }
    }
    }


  • Wayne R

    I'll leave that up to Bob, I'm out of Control keys.


  • ahmedWebDev

    haha
  • Drag Drop Button