Reputation: 3395
I want to make it so that some of my user controls have the ability to 'pop out' into a new window. How I see it working is that the user control will remain where it currently it, but will send a copy of its current state into a new window. I also want this functionality to be in a base class so that derived classes will have this functionality.
Here is what I have so far:
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 - in order to place it on the new form - how can I solve this?
Upvotes: 4
Views: 1283
Reputation: 20044
You should make a copy of the control instead of passing a reference, 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 clone or serialize Windows Forms controls, read this article:
http://www.codeproject.com/Articles/12976/How-to-Clone-Serialize-Copy-Paste-a-Windows-Forms
Upvotes: 3
Reputation: 2439
Your PopOut() has to be changed. Create a clone of the 'this'. Add the cloned object into the new form created. Implement the ICloneable interface in your PopOutControl class. Your clone() method has to be implemented such that it has the same state of your 'PopOutControl' object, ie 'this'.
public void Popout()
{
XtraForm PopoutForm = new XtraForm();
PopoutForm.Controls.Add(this.Clone());
Dock = DockStyle.Fill;
PopoutForm.Show();
}
Upvotes: 1