- Tab Control
- Timer (disabled, intervals of 10)
- A Menu Strip, heres the layout:
- File
- New (CTRL + N)
- Open(CTRL + O)
- Save (CTRL + S)
- Save As
- Exit
- New (CTRL + N)
- Edit
- Undo (CTRL + Z)
- Redo (CTRL + Y)
- Cut(CTRL + X)
- Copy(CTRL + C)
- Paste(CTRL + V)
- Delete(Del)
- Select All(CTRL + A)
- Undo (CTRL + Z)
- Tab
- Close Tab
- File
Heres what your form should look like

Okay lets get started. Right after you decalre your class you'll need two variables to be used as place holders. Here they are
Dim Tab As TabPage 'Holder for the currently selected tab page
Dim RTB As RichTextBox 'Holder for the current RichTextBox
Next we'll have a function used to add tabs, respectively it's called AddTab(). You'll notice it has an optional parameter, file, this is so that the function can be used for when the user presses the Open button. Heres the function
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
Next up is the form load, pretty simple, adds a tab to the tabcontrol so it's there when the user starts up the program and then enabled our timer which checks the current richtextbox to see if certain function should be available or not, here you go
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddTab() 'Add a new tab by default
Timer1.Enabled = True 'Start checking functions after adding the tab
End Sub
Next is the form closing, this will go though each tab and ask the user if he/she would like to save so they're not losing anything important, if the users says yes then it saves and moves on to the next tab, if the user says no then it won't save and moves on, and if he/she clicks cancel then if will stop going through the tabs and wont close the program
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If TabControl1.TabPages.Count > 0 Then 'If there is atleast 1 tab
For Each Tab As TabPage In TabControl1.TabPages 'Go though tabs
TabControl1.SelectedTab = Tab 'Selct that tab
Dim msg As DialogResult = MessageBox.Show("Would you like to save first?", "Save", MessageBoxButtons.YesNoCancel) 'Create a message box asking to save
If msg = DialogResult.Yes Then 'If the user presses yes
SaveToolStripMenuItem_Click() 'Save
ElseIf msg = DialogResult.Cancel Then 'If the user presses cancel
e.Cancel = True 'Stop closing the form
End If
Next
Else 'If there are less than 1 tab
Application.Exit() 'Exit the program
End If
End Sub
The next bit handles when the selected tab is changed and if there are no tabs then it disabled the timer and the buttons used on the richtextbox if there are tabs then it enabled the timer and several buttons as well as updates the selected tab (Tab) and the current richtextbox (RTB)
Private Sub TabControl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
If TabControl1.TabCount > 0 Then 'If there are tabs
Tab = TabControl1.SelectedTab 'Update the current Tab
RTB = TabControl1.SelectedTab.Tag 'Update the current RichTextBox
RTB.Focus() ' Focus on the richtextbox
Timer1.Enabled = True 'enable the timer
SaveToolStripMenuItem.Enabled = True 'Enable save button
SaveAsToolStripMenuItem.Enabled = True 'Enable saveas button
CloseTabToolStripMenuItem.Enabled = True 'Enable closetab button
Else
Timer1.Enabled = False 'disable the timer
UndoToolStripMenuItem.Enabled = False 'diable undo button
RedoToolStripMenuItem.Enabled = False 'disable redo button
CopyToolStripMenuItem.Enabled = False 'disable copy button
CutToolStripMenuItem.Enabled = False 'disable cut button
PasteToolStripMenuItem.Enabled = False 'enable paste button
DeleteToolStripMenuItem.Enabled = False 'disable delete button
SeletAllToolStripMenuItem.Enabled = False 'disable select all button
SaveToolStripMenuItem.Enabled = False 'Disable save button
SaveAsToolStripMenuItem.Enabled = False 'disable saveas button
CloseTabToolStripMenuItem.Enabled = False 'disable closetab button
End If
End Sub
Next is the code for when the user presses the New button, it just adds a new tab
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
AddTab() 'Add a new tab
End Sub
Now for the Open button, when pressed it will open an openfiledialog and then load the selected file
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
Dim open As New OpenFileDialog 'Create a new openfiledialog
If open.ShowDialog = DialogResult.OK Then 'If the user selects a file and hit "Ok"
AddTab(open.FileName) 'Add a new tab for the file
End If
End Sub
Now for the Save button, this will check if the tab was an open file or if it has been saved if it neither of these then it will run the save as
Private Sub SaveToolStripMenuItem_Click() Handles SaveToolStripMenuItem.Click
If Not IsNothing(RTB.Tag) Then
Dim file As String = RTB.Tag 'Assign the filename to a string for easier use
If My.Computer.FileSystem.FileExists(file) Then
Dim reader As New System.IO.StreamReader(file) 'Create a reader ro read the file
Dim RTBCurText = reader.ReadToEnd 'Get the files current text
reader.Close() 'Close the reader
If Not RTB.Text = RTBCurText Then 'If the richtextbox's text is different
Dim writer As New System.IO.StreamWriter(file, False) 'Create a writer to write to the file
writer.Write(RTB.Text) 'Write the richtextbox's content to the file
writer.Close() ' Close the writer
End If
Else 'If the file doesn't exist
SaveAsToolStripMenuItem_Click() 'Execute the SaveAsToolStripMenuItem_Click() function
End If
Else 'The tag hasn't been set yet
SaveAsToolStripMenuItem_Click() 'Execute the SaveAsToolStripMenuItem_Click() function
End If
End Sub
And heres the SaveAs button
Private Sub SaveAsToolStripMenuItem_Click() Handles SaveAsToolStripMenuItem.Click
Dim saveas As New SaveFileDialog 'Create a SaveFileDialog
If saveas.ShowDialog = DialogResult.OK Then 'If the user names the file and hits Ok
Dim writer As New System.IO.StreamWriter(saveas.FileName, False) 'Create a writer to write to the file
writer.Write(RTB.Text) 'Write the richtextbox's content to the file
writer.Close() ' Close the writer
RTB.Tag = saveas.FileName 'Save the filename for later use
Tab.Text = My.Computer.FileSystem.GetFileInfo(saveas.FileName).Name 'Update the tab's text
End If
End Sub
Next up are the Undo, Redo, Cut, Copy, Paste, Delete, and Select All button, these are as simple as it gets so here you go
Private Sub UndoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UndoToolStripMenuItem.Click
RTB.Undo() 'Undo the last thing in the current RichTextBox
End Sub
Private Sub RedoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RedoToolStripMenuItem.Click
RTB.Redo() 'Redo the last thing in the current RichTextBox
End Sub
Private Sub CutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CutToolStripMenuItem.Click
RTB.Cut() 'Cut the seleted text in the current RichTextBox
End Sub
Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click
RTB.Copy() 'Copy the seleted text in the current RichTextBox
End Sub
Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteToolStripMenuItem.Click
RTB.Paste() 'Paste into the current RichTextBox
End Sub
Private Sub DeleteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteToolStripMenuItem.Click
RTB.SelectedText = "" 'Delete the seleted text in the current RichTextBox
End Sub
Private Sub SeletAllToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SeletAllToolStripMenuItem.Click
RTB.SelectAll() 'Seleect all in the current RichTextBox
End Sub
Next up is your Close Tab button this basically does the same thing as when you exit the form only it does it for one tab
Private Sub CloseTabToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseTabToolStripMenuItem.Click
Dim msg As DialogResult = MessageBox.Show("Would you like to save first?", "Save", MessageBoxButtons.YesNoCancel) 'Create a message box asking to save
If msg = DialogResult.Yes Then 'If user presses yes
SaveToolStripMenuItem_Click() 'Save
TabControl1.TabPages.Remove(Tab) 'Then remove tab
ElseIf msg = DialogResult.No Then 'If user presses no
TabControl1.TabPages.Remove(Tab) 'Remove tab without saving
End If
End Sub
And last but not least, one of the most important pieces that without the whole program will crash, the timer than enables/disabled the control button for the richtextbox, first it will check each function then either enable/disable if depending on the result, this is run ever 10 milliseconds
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If RTB.CanUndo Then 'If current richtextbox can undo
UndoToolStripMenuItem.Enabled = True 'enable undo button
Else
UndoToolStripMenuItem.Enabled = False 'diable undo button
End If
If RTB.CanRedo Then 'If current richtextbox can redo
RedoToolStripMenuItem.Enabled = True 'enable redo button
Else
RedoToolStripMenuItem.Enabled = False 'disable redo button
End If
If RTB.SelectedText.Length > 0 Then 'If the selected text of the current richtextbox is longer than 0
CopyToolStripMenuItem.Enabled = True 'enable copy button
CutToolStripMenuItem.Enabled = True 'enable cut button
DeleteToolStripMenuItem.Enabled = True 'enable delete button
Else
CopyToolStripMenuItem.Enabled = False 'disable copy button
CutToolStripMenuItem.Enabled = False 'disable cut button
DeleteToolStripMenuItem.Enabled = False 'disable delete button
End If
If My.Computer.Clipboard.ContainsText Then 'If there is text copied to the clipboard
PasteToolStripMenuItem.Enabled = True 'enable paste button
Else
PasteToolStripMenuItem.Enabled = False 'disable paste button
End If
If RTB.Text.Length > 0 Then 'If the text of the current richtextbox is longer than 0
SeletAllToolStripMenuItem.Enabled = True 'enable select all button
Else
SeletAllToolStripMenuItem.Enabled = False 'disable select all button
End If
End Sub
So thats all the code, heres what the code should look like when you are done
Public Class Form1
Dim Tab As TabPage 'Holder for the currently selected tab page
Dim RTB As RichTextBox 'Holder for the current RichTextBox
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
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddTab() 'Add a new tab by default
Timer1.Enabled = True 'Start checking functions after adding the tab
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If TabControl1.TabPages.Count > 0 Then 'If there is atleast 1 tab
For Each Tab As TabPage In TabControl1.TabPages 'Go though tabs
TabControl1.SelectedTab = Tab 'Selct that tab
Dim msg As DialogResult = MessageBox.Show("Would you like to save first?", "Save", MessageBoxButtons.YesNoCancel) 'Create a message box asking to save
If msg = DialogResult.Yes Then 'If the user presses yes
SaveToolStripMenuItem_Click() 'Save
ElseIf msg = DialogResult.Cancel Then 'If the user presses cancel
e.Cancel = True 'Stop closing the form
End If
Next
Else 'If there are less than 1 tab
Application.Exit() 'Exit the program
End If
End Sub
Private Sub TabControl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
If TabControl1.TabCount > 0 Then 'If there are tabs
Tab = TabControl1.SelectedTab 'Update the current Tab
RTB = TabControl1.SelectedTab.Tag 'Update the current RichTextBox
RTB.Focus() ' Focus on the richtextbox
Timer1.Enabled = True 'enable the timer
SaveToolStripMenuItem.Enabled = True 'Enable save button
SaveAsToolStripMenuItem.Enabled = True 'Enable saveas button
CloseTabToolStripMenuItem.Enabled = True 'Enable closetab button
Else
Timer1.Enabled = False 'disable the timer
UndoToolStripMenuItem.Enabled = False 'diable undo button
RedoToolStripMenuItem.Enabled = False 'disable redo button
CopyToolStripMenuItem.Enabled = False 'disable copy button
CutToolStripMenuItem.Enabled = False 'disable cut button
PasteToolStripMenuItem.Enabled = False 'enable paste button
DeleteToolStripMenuItem.Enabled = False 'disable delete button
SeletAllToolStripMenuItem.Enabled = False 'disable select all button
SaveToolStripMenuItem.Enabled = False 'Disable save button
SaveAsToolStripMenuItem.Enabled = False 'disable saveas button
CloseTabToolStripMenuItem.Enabled = False 'disable closetab button
End If
End Sub
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
AddTab() 'Add a new tab
End Sub
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
Dim open As New OpenFileDialog 'Create a new openfiledialog
If open.ShowDialog = DialogResult.OK Then 'If the user selects a file and hit "Ok"
AddTab(open.FileName) 'Add a new tab for the file
End If
End Sub
Private Sub SaveToolStripMenuItem_Click() Handles SaveToolStripMenuItem.Click
If Not IsNothing(RTB.Tag) Then
Dim file As String = RTB.Tag 'Assign the filename to a string for easier use
If My.Computer.FileSystem.FileExists(file) Then
Dim reader As New System.IO.StreamReader(file) 'Create a reader ro read the file
Dim RTBCurText = reader.ReadToEnd 'Get the files current text
reader.Close() 'Close the reader
If Not RTB.Text = RTBCurText Then 'If the richtextbox's text is different
Dim writer As New System.IO.StreamWriter(file, False) 'Create a writer to write to the file
writer.Write(RTB.Text) 'Write the richtextbox's content to the file
writer.Close() ' Close the writer
End If
Else 'If the file doesn't exist
SaveAsToolStripMenuItem_Click() 'Execute the SaveAsToolStripMenuItem_Click() function
End If
Else 'The tag hasn't been set yet
SaveAsToolStripMenuItem_Click() 'Execute the SaveAsToolStripMenuItem_Click() function
End If
End Sub
Private Sub SaveAsToolStripMenuItem_Click() Handles SaveAsToolStripMenuItem.Click
Dim saveas As New SaveFileDialog 'Create a SaveFileDialog
If saveas.ShowDialog = DialogResult.OK Then 'If the user names the file and hits Ok
Dim writer As New System.IO.StreamWriter(saveas.FileName, False) 'Create a writer to write to the file
writer.Write(RTB.Text) 'Write the richtextbox's content to the file
writer.Close() ' Close the writer
RTB.Tag = saveas.FileName 'Save the filename for later use
Tab.Text = My.Computer.FileSystem.GetFileInfo(saveas.FileName).Name 'Update the tab's text
End If
End Sub
Private Sub UndoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UndoToolStripMenuItem.Click
RTB.Undo() 'Undo the last thing in the current RichTextBox
End Sub
Private Sub RedoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RedoToolStripMenuItem.Click
RTB.Redo() 'Redo the last thing in the current RichTextBox
End Sub
Private Sub CutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CutToolStripMenuItem.Click
RTB.Cut() 'Cut the seleted text in the current RichTextBox
End Sub
Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click
RTB.Copy() 'Copy the seleted text in the current RichTextBox
End Sub
Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteToolStripMenuItem.Click
RTB.Paste() 'Paste into the current RichTextBox
End Sub
Private Sub DeleteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteToolStripMenuItem.Click
RTB.SelectedText = "" 'Delete the seleted text in the current RichTextBox
End Sub
Private Sub SeletAllToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SeletAllToolStripMenuItem.Click
RTB.SelectAll() 'Seleect all in the current RichTextBox
End Sub
Private Sub CloseTabToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseTabToolStripMenuItem.Click
Dim msg As DialogResult = MessageBox.Show("Would you like to save first?", "Save", MessageBoxButtons.YesNoCancel) 'Create a message box asking to save
If msg = DialogResult.Yes Then 'If user presses yes
SaveToolStripMenuItem_Click() 'Save
TabControl1.TabPages.Remove(Tab) 'Then remove tab
ElseIf msg = DialogResult.No Then 'If user presses no
TabControl1.TabPages.Remove(Tab) 'Remove tab without saving
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If RTB.CanUndo Then 'If current richtextbox can undo
UndoToolStripMenuItem.Enabled = True 'enable undo button
Else
UndoToolStripMenuItem.Enabled = False 'diable undo button
End If
If RTB.CanRedo Then 'If current richtextbox can redo
RedoToolStripMenuItem.Enabled = True 'enable redo button
Else
RedoToolStripMenuItem.Enabled = False 'disable redo button
End If
If RTB.SelectedText.Length > 0 Then 'If the selected text of the current richtextbox is longer than 0
CopyToolStripMenuItem.Enabled = True 'enable copy button
CutToolStripMenuItem.Enabled = True 'enable cut button
DeleteToolStripMenuItem.Enabled = True 'enable delete button
Else
CopyToolStripMenuItem.Enabled = False 'disable copy button
CutToolStripMenuItem.Enabled = False 'disable cut button
DeleteToolStripMenuItem.Enabled = False 'disable delete button
End If
If My.Computer.Clipboard.ContainsText Then 'If there is text copied to the clipboard
PasteToolStripMenuItem.Enabled = True 'enable paste button
Else
PasteToolStripMenuItem.Enabled = False 'disable paste button
End If
If RTB.Text.Length > 0 Then 'If the text of the current richtextbox is longer than 0
SeletAllToolStripMenuItem.Enabled = True 'enable select all button
Else
SeletAllToolStripMenuItem.Enabled = False 'disable select all button
End If
End Sub
End Class
And there you have it a completed Tabbed Notepad, certainly a lot better than notepad. It's also easily expandable to ass more thing like changing font, background color, foreground color, so give it a try and let me know what you think.
Good Luck,
Daniel






MultiQuote





|