473,809 Members | 2,591 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Prathap
37 New Member
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 7978
GaryTexmo
1,501 Recognized Expert Top Contributor
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
4343
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 control (label1) in Form1 from Form2? Thanks Andrew
4
428
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 to save the person's name in code behind protected void SavePersonInfo(Person p)
0
1543
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 problem? PS: I don't need a code example showing me how to do it without drag 'n' drop because I already know how. I WANT to learn how to do it with drag 'n' drop.
1
1053
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 bottom of the page where I want them. How can I do this? Thanks, Trint Net programmer trintsmith@hotmail.com
3
4762
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 property of a listviewitem from another thread throws an exception! Is there any way to allow these as invoking the host thread to do the work is going to really slow things down unless I change the architechture completely. Wouldn't it have made...
3
9400
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
34050
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. My company has two identical web sites. One copy is for our customer, and one copy is for us to test our code changes on. We developed a hard to isolate bug in the copy of the web stie for our
5
5793
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 table. So for psuedo-code it would be something like: update Mapping Set SID = (select SMappingID from Mapping where SMappingID is not null) I don't know if I should be doing this in a loop (which I'm not totally familiar with using) or if there...
16
4179
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 therefore being able to modify its properties?
4
4805
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 error in Spanish (I don't get translation: Los controles creados en un subproceso no pueden tener controles primarios en un control en un subproceso diferente.)
0
9603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10643
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10378
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10391
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3862
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.