473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

Prathap
37
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
Expand|Select|Wrap|Line Numbers
  1.  InitializeComponent();
  2.             this.panel2.DragOver += new DragEventHandler(panel2_DragOver);
  3.             this.panel2.DragDrop += new DragEventHandler(panel2_DragDrop);
  4.  
  5.  
  6.             panel2.AllowDrop = true;
  7.             panel1.AllowDrop = true;
  8.  
  9.             foreach (Control ctrl in this.panel1.Controls)
  10.             {
  11.                 ctrl.MouseDown += new MouseEventHandler(button1_MouseDown);
  12.             }
  13.             foreach (Control ctrl1 in this.panel2.Controls)
  14.             {
  15.                 ctrl1.MouseDown += new MouseEventHandler(button1_MouseDown);
  16.             }   
  17.         }
  18.  
  19.         private void button1_MouseDown(object sender, MouseEventArgs e)
  20.         {
  21.  
  22.             Control ctrl1 = sender as Control;
  23.             ctrl1.DoDragDrop(ctrl1, DragDropEffects.Copy);
  24.         }
  25.  
  26.         private void panel2_DragDrop(object sender, DragEventArgs e)
  27.         {
  28.             Control ctrl = e.Data.GetData(e.Data.GetFormats()[0]) as Control;
  29.             if (ctrl != null)
  30.             {
  31.                 ctrl.Location = this.panel2.PointToClient(new Point(e.X, e.Y));
  32.                 this.panel2.Controls.Add(ctrl);
  33.             }
  34.            // panel2.BackColor = Color.Aquamarine;
  35.  
  36.         }
  37.  
  38.         private void panel1_DragDrop(object sender, DragEventArgs e)
  39.         {
  40.             Control ctrl1 = e.Data.GetData(e.Data.GetFormats()[0]) as Control;
  41.             if (ctrl1 != null)
  42.             {
  43.                 ctrl1.Location = this.panel1.PointToClient(new Point(e.X, e.Y));
  44.                 this.panel1.Controls.Add(ctrl1);
  45.             }
  46.            // panel1.BackColor = Color.DarkMagenta;
  47.         }
  48.  
  49.         private void panel2_DragOver(object sender, DragEventArgs e)
  50.         {
  51.             e.Effect = DragDropEffects.Copy;
  52.         }
  53.  
  54.         private void panel1_DragOver(object sender, DragEventArgs e)
  55.         {
  56.             e.Effect = DragDropEffects.Copy;
  57.         }
Feb 21 '12 #1
1 7908
GaryTexmo
1,501 Expert 1GB
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...

Expand|Select|Wrap|Line Numbers
  1. public class ControlCloner<T>
  2. {
  3.   public T CloneObject(T sourceObject)
  4.   {
  5.     T newObject = new T();
  6.  
  7.     // Set properties & events of newObject using reflection... look at the methods available on the Type class.
  8.  
  9.     return newObject;
  10.   }
  11. }
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 :)
Feb 21 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Andrew Diabo | last post by:
I have 2 forms (Form1 and Form2) in my C# project. I created the second form from the main form like so: Fom2 aForm = new Form2(); aForm.ShowDialog(); How do I access the properties of a...
4
by: David | last post by:
I have trying to have a webform inherit controls from another form and can't get it to work Say I have a form that saves the person's demographic info. ****one.aspx**** //I have an object...
0
by: Oberon | last post by:
How do I navigate through records using drag 'n' drop controls? I have an example in C# for Windows forms that relies on BindingContext but this is not available for ASP.NET. How do I solve that...
1
by: Trint Smith | last post by:
In vs.net I have a webform with controls I neet to copy to another page...when I cut and paste them, the are on top of the destination pages controls and I can't re-select them to drag them to the...
3
by: NickP | last post by:
Hi there, This might sound like a stupid question but this whole exception being thrown when accessing user controls from another thread is extremely over the top! Just looking at the index...
3
by: Christopher Lusardi | last post by:
If I want to copy one file to another how do I do that? I want to do this even if the second file already exists. Thanks, Christopher Lusardi
5
by: Steve | last post by:
Hi; I thought I would rephrase a question I asked in another post to more quickly get to the heart of the matter. My apologies for anyone who is offended by what appears to be a repetition. ...
5
by: NamelessNumberheadMan | last post by:
I have a table I need to make changes to. Dropping columns isn't my problem, but before I drop one of the columns I need to copy the values that aren't null to another existing column in the same...
16
by: Mike | last post by:
Hi, I have a form with some controls, and a different class that needs to modify some control properties at run time. Hoy can I reference the from so I have access to its controls and...
4
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post by:
Hi misters, I have winForm that needs update Panel (add controls to Panel of SplitContainer dynamically). I have a Backgroundworker, and DoWork event I want to add controls but I get this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.