How to Copy and drop controls from one panel to another?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Prathap
    New Member
    • Nov 2011
    • 37

    How to Copy and drop controls from one panel to another?

    Drag and drop controls from one panel to another is working.I want to drop only a copy of the control.Could someone please tell me what are the changes should i made to get it to work.Here is the code i have done so far
    Code:
     InitializeComponent();
                this.panel2.DragOver += new DragEventHandler(panel2_DragOver);
                this.panel2.DragDrop += new DragEventHandler(panel2_DragDrop);
    
               
                panel2.AllowDrop = true;
                panel1.AllowDrop = true;
    
                foreach (Control ctrl in this.panel1.Controls)
                {
                    ctrl.MouseDown += new MouseEventHandler(button1_MouseDown);
                }
                foreach (Control ctrl1 in this.panel2.Controls)
                {
                    ctrl1.MouseDown += new MouseEventHandler(button1_MouseDown);
                }   
            }
    
            private void button1_MouseDown(object sender, MouseEventArgs e)
            {
    
                Control ctrl1 = sender as Control;
                ctrl1.DoDragDrop(ctrl1, DragDropEffects.Copy);
            }
    
            private void panel2_DragDrop(object sender, DragEventArgs e)
            {
                Control ctrl = e.Data.GetData(e.Data.GetFormats()[0]) as Control;
                if (ctrl != null)
                {
                    ctrl.Location = this.panel2.PointToClient(new Point(e.X, e.Y));
                    this.panel2.Controls.Add(ctrl);
                }
               // panel2.BackColor = Color.Aquamarine;
               
            }
    
            private void panel1_DragDrop(object sender, DragEventArgs e)
            {
                Control ctrl1 = e.Data.GetData(e.Data.GetFormats()[0]) as Control;
                if (ctrl1 != null)
                {
                    ctrl1.Location = this.panel1.PointToClient(new Point(e.X, e.Y));
                    this.panel1.Controls.Add(ctrl1);
                }
               // panel1.BackColor = Color.DarkMagenta;
            }
    
            private void panel2_DragOver(object sender, DragEventArgs e)
            {
                e.Effect = DragDropEffects.Copy;
            }
    
            private void panel1_DragOver(object sender, DragEventArgs e)
            {
                e.Effect = DragDropEffects.Copy;
            }
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    I think you're better off doing this yourself. There is a built in way... kind of... an Object has a protected method called MemberwiseClone which is supposed to do a high level clone of all the members on your object. You can read about it here: http://msdn.microsoft.com/en-us/libr...cloneable.aspx

    That said, I gave it a try with a button and couldn't get it to show up on my form. It said it was there, but it wasn't. It was very confusing.

    So my advice would be to make something yourself. You can look into reflection if you want in order to try to duplicate what MemberwiseClone does, but make it work properly. Perhaps using a structure like this...

    Code:
    public class ControlCloner<T>
    {
      public T CloneObject(T sourceObject)
      {
        T newObject = new T();
    
        // Set properties & events of newObject using reflection... look at the methods available on the Type class.
    
        return newObject;
      }
    }
    If you can't do it with reflection you might just have to do it manually, creating either cases in a switch using the above structure or simply creating new derived classes for each of the controls you support and cloning them.

    Sorry, I know this is likely not what you're looking for and there may well be a better way, but I don't know it. If you figure out something better, please post it back here so people reading this thread can see what you did :)

    Comment

    Working...