I wrote for you a simple class to add and removes tabs (with forms). The solution is not perfect... You need to remember: this is an example!
Steps to achieve a solution:
1) We need an
interface[
^]
Public Interface IFormsInTabs
Function AddTab(ByVal frm As Windows.Forms.Form) As Integer
Function RemoveTab() As Integer
End Interface
2) We need a
class[
^] which implements methods, functions (and other stuff) from interface.
Public Class TFormsInTabs
Implements IFormsInTabs
Private oTc As TabControl = Nothing
Public Sub New(ByVal _tc As TabControl)
oTc = _tc
End Sub
Public Function AddTab(ByVal frm As System.Windows.Forms.Form) As Integer Implements IFormsInTabs.AddTab
Dim oTp As TabPage = Nothing
Try
oTp = New TabPage(frm.Text)
frm.TopLevel = False
oTp.Controls.Add(frm)
frm.Dock = DockStyle.Fill
frm.Show()
oTc.TabPages.Add(oTp)
oTc.SelectedTab = oTp
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error... {AddTab}")
Finally
oTp = Nothing
End Try
End Function
Public Function RemoveTab() As Integer Implements IFormsInTabs.RemoveTab
Try
oTc.TabPages.Remove(oTc.SelectedTab)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error... {RemoveTab}")
End Try
End Function
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
End Class
3) Implemetation (in the form with TabControl):
Public Class MainFrm
Dim mTC As IFormsInTabs = Nothing
Public Sub New()
InitializeComponent()
mTC = New TFormsInTabs(Me.TabControl1)
End Sub
Private Sub TSMIOpenForm1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMIOpenForm1.Click
Dim frm As Form1 = New Form1
mTC.AddTab(frm)
Me.TabControl1.Refresh()
End Sub
Private Sub TSMIOpenForm2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMIOpenForm2.Click
Dim frm As Form2 = New Form2
mTC.AddTab(frm)
Me.TabControl1.Refresh()
End Sub
Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
mTC.RemoveTab()
Me.TabControl1.Refresh()
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
End Class
I hope it will be helpful (if yes, please rate it). If you have any question, please let me know (use "Have a Question ..." widget).