What i want to do:
Dragging and docking the first form in the second form works. But i can't get the form back out of the second form. When i drag it out, it disappears.
Because the dragging and dropping makes the example code more complicated than necessary i left it out. Instead i used buttons. The example form has two buttons and a panel. When button one is pressed the form puts a copy of itself in the panel. When button two is pressed the copy form should be undocked and showing as a separate form. This last part doesn't work. Instead the copy form disappears.
Is what i am trying to do not possible, or am i doing something wrong?
EDIT: from Hans' comment now i know what's wrong. I changed the code below to make it work. I put a comment behind the code line that is changed
public partial class Form1 : Form
{
private Form1 CopyForm;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (CopyForm == null)
{
CopyForm = new Form1();
CopyForm.TopLevel = false;
CopyForm.Parent = panel1;
CopyForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
CopyForm.Dock = DockStyle.Fill;
CopyForm.Show();
}
}
private void button2_Click(object sender, EventArgs e)
{
CopyForm.Dock = DockStyle.None;
CopyForm.Parent = null;
panel1.Controls.Clear();
CopyForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
CopyForm.StartPosition = FormStartPosition.CenterScreen;
CopyForm.Size = new Size(500, 500);
CopyForm.TopMost = true;
CopyForm.TopLevel = true; // <== This line was the fix to my problem
CopyForm.Show();
}
}
As Hans Passant pointed out,
CopyForm.TopLevel was not set to true in the button2_Click method.
Adding CopyForm.TopLevel = true;
made the form show up!
Thanks Hans!