Private Sub AddTab(Optional ByVal file As String = Nothing)
Dim NewTab As New TabPage 'Create a new tab
Dim NewRTB As New RichTextBox 'Create a new richtextbox
Dim TabText As String = "Untitled" 'Text for the tab. Default is "Untitled"
Dim RTBText As String = Nothing 'Text for the richtextbox. Default is Nothing
If Not IsNothing(file) Then 'If a file was set
If My.Computer.FileSystem.FileExists(file) Then 'If the file exists
NewRTB.Tag = file 'Set the file as the new richtextbox's tag for later use
TabText = My.Computer.FileSystem.GetFileInfo(file).Name 'Get the filename
Dim reader As New System.IO.StreamReader(file) 'Create a reader to read the file
RTBText = reader.ReadToEnd 'Read all the text of the file
reader.Close() ' Close the reader
Else
MsgBox("Sorry that file doesn't exist") 'Inform the user if the file doesn't exist
End If
End If
NewTab.Text = TabText 'Set the tabs text
NewTab.Tag = NewRTB 'Set the richtextbox as the Tab's tag for later use
NewRTB.Parent = NewTab 'Make the richtextbox a child of the tag
NewRTB.Dock = DockStyle.Fill 'Make the richtextbox fill the tabpage
NewRTB.Text = RTBText 'Set the richtextbox's text
TabControl1.TabPages.Add(NewTab) 'Add the tab to the tabcontrol
TabControl1.SelectedTab = NewTab 'Focus on the new tab
Tab = NewTab 'Set the current tab to NewTab
RTB = NewRTB 'Set the current richtextbox to NewRTB
End Sub