Display user control in a new window

I want to make some of my custom controls pop up into a new window. As I see it, it works, as the user control will remain where it is, but will send a copy of its current state to a new window. I also want this functionality to be in the base class, so derived classes will have this functionality.

Here is what I still have:

public class PopoutControl : XtraUserControl { public void Popout() { XtraForm PopoutForm = new XtraForm(); PopoutForm.Controls.Add(this); Dock = DockStyle.Fill; PopoutForm.Show(); } } public partial class PopoutControlTest : PopoutControl { public PopoutControlTest() { InitializeComponent(); } private void OnPopoutRequest(object sender, EventArgs e) { Popout(); } } 

This works, except that it removes the user control from the original form, where it is located - to place it in the new form - how can I solve it?

  • William
+4
source share
2 answers

You should make a copy of the control instead of passing the link, for example, by implementing some Clone method:

 public class PopoutControl : XtraUserControl { public void Popout() { XtraForm PopoutForm = new XtraForm(); PopoutForm.Controls.Add(this.Clone()); Dock = DockStyle.Fill; PopoutForm.Show(); } public PopoutControl Clone() { var p = new PopoutControl(); // implement copying of the current state to p here // ... return p; } } 

EDIT: For a general approach to cloning or serializing Windows Forms controls, read this article:

http://www.codeproject.com/Articles/12976/How-to-Clone-Serialize-Copy-Paste-a-Windows-Forms

+3
source

Your PopOut () needs to be changed. Create a clone of 'this'. Add the cloned object to the newly created form. Embed the ICloneable interface in your PopOutControl class. Your clone () method must be implemented in such a way that it has the same state as your PopOutControl object, i.e. 'this'.

 public void Popout() { XtraForm PopoutForm = new XtraForm(); PopoutForm.Controls.Add(this.Clone()); Dock = DockStyle.Fill; PopoutForm.Show(); } 
+1
source

All Articles