Click here to Skip to main content
15,850,446 members
Please Sign up or sign in to vote.
1.73/5 (3 votes)
See more:
Hello guys!

Im trying to do a simple drag and drop, from a panel to another panel...But what im trying to drag is a button...

The problem is that my code moves the control, not copy.. I want to maintain the source button...

here is my code:

C#
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 DragDropExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void panel1_DragDrop(object sender, DragEventArgs e)
        {
            
            Control ctrl = e.Data.GetData(e.Data.GetFormats()[0]) as Control;
            if (ctrl != null)
            {
                ctrl.Location = this.panel1.PointToClient(new Point(e.X, e.Y));
                this.panel1.Controls.Add(ctrl);

            }

        }

        private void button1_MouseDown(object sender, MouseEventArgs e)
        {
            //button1.DoDragDrop(button1, DragDropEffects.Copy | DragDropEffects.Move);
            button1.DoDragDrop(button1, DragDropEffects.Copy );
        }

        private void panel1_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy; 
        }

        private void Form1_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy ; 
        }


        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            Control ctrl = e.Data.GetData(e.Data.GetFormats()[0]) as Control;
            if (ctrl != null)
            {
                ctrl.Location = this.PointToClient(new Point(e.X, e.Y));
                this.Controls.Add(ctrl);
            }   
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
    }
}


thanksss!!


Rafael
Posted
Updated 20-Sep-18 13:33pm
Comments
idenizeni 8-Oct-13 15:50pm    
So, you want to use drag and drop functionally to clone a button and not actually drag and drop the button?
BillWoodruff 8-Oct-13 19:50pm    
Are you looking for a "general solution" that you can use to move/copy any Control, or a specific solution for this particular case of moving/duplicating a Button from one Panel to another ? Have you examined what's on CP, and the web, about duplicating Controls at run-time, like: http://www.codeproject.com/Articles/12976/How-to-Clone-Serialize-Copy-Paste-a-Windows-Forms

1 solution

What I understand from your description of your problem, & what I see from your code is :
You want to copy the control, but what you got is, the control moved.
Solution is simple :
Replace every statement like :
Controls.Add(Ctrl);
To :
Control Ctrl2 = nCtrl.Clone();
Controls.Add(Ctrl2);
 
Share this answer
 
Comments
Dave Kreskowiak 20-Sep-18 18:48pm    
...from FIVE YEARS AGO.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900