views:

52

answers:

2

Hi, I have a MainForm which has tab Control and several independent form. I open each Individual From in the tab of the main form. A "Close Tab" button on the MainFrom closes the current tab, its implementation is below.

This closes the current tab but what I also need is to dispose the From whose tab is closed but I am not sure how to get the instance of the form.'

Any help is appreciated Thanks in Advance.

'Close the current tab
  Private Sub CloseCurrentTab()
    'Close the current tab 

      Dim tabPageSave As TabPage
      tabPageSave = tcDisplayDetails.SelectedTab
      tcDisplayDetails.TabPages.Remove(tabPageSave)

End Sub

A: 

I'd suggest keeping a reference to the forms in the main form. So I'd have a frmPageSave and when I open the form for the PageSave tab I store the reference in that variable and then in your CloseCurrentTab method I'd add something like:

IF frmPageSave IsNot Nothing Then
    frmPageSave.Close()
    frmPageSave = Nothing
End If
ho1
+1  A: 

Yes, you'll need to dispose all of the controls in the tab page. The form isn't special, it is just a child control when you set its TopLevel property to false. Make it look like this:

  Dim tabPageSave As TabPage
  tabPageSave = tcDisplayDetails.SelectedTab
  While tabPageSave.Controls.Count > 0
      tabPageSave.Controls(0).Dispose()
  End While
  tcDisplayDetails.TabPages.Remove(tabPageSave)

The form's Dispose() method will automatically dispose any child controls it has, no additional help is needed.

Hans Passant