title: VB.NET Docking Form on Panel in Tab Page
body: <p>how to add a form in a panel inside tab page ?
  i use this code to do it, but when i resize my main form, the form inside tab page doesn't auto resize.</p>
  
  <pre><code>Public Sub showForm(ByVal frm As MetroForm)
      Dim a As New Panel
  
      For i As Integer = 0 To tabPage.TabCount - 1
          If frm.Text = tabPage.TabPages(i).Text Then
              tabPage.SelectedIndex = i
              Exit Sub
          End If
      Next
  
      a = New Panel
      tabPage.TabPages.Add("")
      tabPage.TabPages(tabPage.TabPages.Count - 1).Controls.Add(a)
      a.Dock = DockStyle.Fill
      frm.TopLevel = False
      frm.MinimizeBox = False
      frm.MaximizeBox = False
      frm.DisplayHeader = False
      frm.AutoSize = False
      frm.Dock = DockStyle.Fill
  
      tabPage.TabPages(tabPage.TabPages.Count - 1).Text = frm.Text
      a.Visible = True
      a.Name = "pnl" &amp; frm.Name
      a.Controls.Add(frm)
      frm.Dock = DockStyle.Fill
      frm.Visible = True
      frm.BringToFront()
  
      AddHandler a.ControlRemoved, AddressOf Me.removePanel
      tabPage.SelectedIndex = tabPage.TabPages.Count - 1
      tabPage.TabPages(tabPage.TabPages.Count - 1).Name = frm.Text
  End Sub
  </code></pre>
  
  <p>and to remove the tab page when form is closed :</p>
  
  <pre><code>Private Sub removePanel(ByVal sender As System.Object, ByVal e As System.EventArgs)
      Dim a As Panel = DirectCast(sender, Panel)
      For Each obj As TabPage In tabPage.TabPages
          If obj.Name = sender.Parent.Name Then
              obj.Controls(0).Dispose() 'remove form
              tabPage.TabPages.Remove(obj) 'remove tab page
              RemoveHandler a.ControlRemoved, AddressOf Me.removePanel
          End If
      Next
  End Sub
  </code></pre>
  
  <p>here's the screenshot : 
  <a href="https://i.stack.imgur.com/zYT1D.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zYT1D.jpg" alt="Before Resize"></a>
  <a href="https://i.stack.imgur.com/JWbh5.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JWbh5.jpg" alt="After Resize"></a></p>