title: Docking form in panel and then un-dock It from same form
body: <p>I open Form2 with button on Form1. With a click on another button in Form2 this form is then added as control in split container's panel of Form1. After that I close form, but split container control is still seen (my Form2). Now I want to perform opposite - to close split container control and open Form2 to normal window as before, again from same button in Form2, in this case control in split container's panel. Is this possible ?</p>
  
  <p>My main purpose of this is to dock Form2 in split container and to undock It, all done from a button on Form2. Here is my code:</p>
  
  <p>EDIT:</p>
  
  <pre><code>Public Class Form2
  
      Dim BtnState As Integer
      Dim frm As Form2
  
      Private Shared Sub DockFormToPanel(panel As Panel, frm As Form)
          frm.TopLevel = False
          frm.WindowState = FormWindowState.Maximized
          frm.FormBorderStyle = FormBorderStyle.None
          frm.Visible = True
          Form1.SplitContainer1.Panel2.Controls.Add(frm)
      End Sub
  
      Private Shared Sub UndockFormFromPanel(panel As Panel, frm As Form)
          If Not Form1.SplitContainer1.Panel2.Controls.Contains(frm) Then Throw New InvalidOperationException()
          Form1.SplitContainer1.Panel2.Controls.Remove(frm)
          frm.Visible = False
          frm.TopLevel = True
          frm.WindowState = FormWindowState.Normal
          frm.FormBorderStyle = FormBorderStyle.Sizable
          frm.Show()
      End Sub
  
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  
          If BtnState = 0 Then
              If frm Is Nothing Then
                  frm = New Form2
                  AddHandler frm.FormClosed, Sub() frm = Nothing
              End If
              DockFormToPanel(Form1.SplitContainer1.Panel2, frm)
              BtnState = 1
          Else
              UndockFormFromPanel(Form1.SplitContainer1.Panel2, frm)
              BtnState = 0
          End If
  
      End Sub
  
  End Class
  </code></pre>