Create customize notepad in Visual Basic.Net
In this tutorial shows how to
create simple and customized notepad based on Visual Basic.Net language. Obviously,
through Visual Basic we can make various applications quickly in an hour or
even minutes. In addition, these program quite the same as usual notepad in our
windows, but I make a little bit customization in my designed and source code.
Some source codes applied in this simple notepad are gathered in different
websites on the internet and add additional features so that it looks like
something different. Added features such as counting the number of characters
and words, recognize HTML, and color. Unfortunately, it’s not really the same
as notepad where all functions are here, this is actually a simple notepad as
what I had intended to work with and not necessarily exactly identical as
notepad. However, if you want to contribute additional features, your comments
plus source code if they have are welcome.
Now, to work with our notepad we
need different controls can be drag on our Windows Form Application. Here are:
MenuStrip, ColorDialog, FontDialog, OpenFileDialog, SaveFileDialog,
PrintDialog, PrintDocument, PrintPreviewDialog, StatusStrip, and ContextMenuStrip.
Note:
When you drag StatusStrip don’t forget to add 2 Status label for counting words
and character and Contextual menu, once you add menu items such as cut, copy,
paste, etc..., click textbox, and on your properties window, locate
contextmenustrip and select the value name MenuStrip1.
Your Form looks like the
following:
Final Output:
Source code for each menu:
New
Dim Save As New SaveFileDialog()
Dim myStreamWriter As
System.IO.StreamWriter
'Check if there's text added to the textbox
If txtTextPad.Modified Then
'If the text of notepad changed, the program will ask the
user if they want to save the changes
Dim ask As MsgBoxResult
ask = MsgBox("Do you want to save the
changes", MsgBoxStyle.YesNoCancel,
"New Document")
If ask = MsgBoxResult.No
Then
txtTextPad.Clear()
ElseIf ask = MsgBoxResult.Cancel
Then
ElseIf ask = MsgBoxResult.Yes
Then
Save.Filter = "Text [*.txt*]|*.txt|All
Files [*.*]|*.*"
Save.CheckPathExists = True
Save.Title = "Save File"
Save.ShowDialog(Me)
Try
myStreamWriter = System.IO.File.AppendText(Save.FileName)
myStreamWriter.Write(txtTextPad.Text)
myStreamWriter.Flush()
Catch ex As
Exception
End Try
txtTextPad.Clear()
End If
'If textbox's text is still the same, notepad will open a
new page:
Else
txtTextPad.Clear()
End If
Open
Dim Save As New SaveFileDialog()
Dim myStreamWriter As
System.IO.StreamWriter
If txtTextPad.Modified Then
'If the text of notepad changed, the program will ask the
user if they want to save the changes
Dim ask As MsgBoxResult
ask = MsgBox("Do you want to save the
changes", MsgBoxStyle.YesNoCancel,
"Open Document")
If ask = MsgBoxResult.No
Then
OpenFileDialog1.ShowDialog()
Try
txtTextPad.Text = My.Computer.FileSystem.ReadAllText(OpenFileDialog1.FileName)
Catch ex As
Exception
End Try
ElseIf ask = MsgBoxResult.Cancel
Then
ElseIf ask = MsgBoxResult.Yes
Then
Save.Filter = "Text [*.txt*]|*.txt|All
Files [*.*]|*.*"
Save.CheckPathExists = True
Save.Title = "Save File"
Save.ShowDialog(Me)
Try
myStreamWriter = System.IO.File.AppendText(Save.FileName)
myStreamWriter.Write(txtTextPad.Text)
myStreamWriter.Flush()
Catch ex As
Exception
End Try
End If
Else
'If textbox's text is still the same, notepad will show the
OpenFileDialog
OpenFileDialog1.ShowDialog()
txtTextPad.Text = My.Computer.FileSystem.ReadAllText(OpenFileDialog1.FileName)
End If
Save
Dim Save As New SaveFileDialog()
Dim myStreamWriter As
System.IO.StreamWriter
Save.Filter = "Text [*.txt*]|*.txt|All
Files [*.*]|*.*"
Save.CheckPathExists = True
Save.Title = "Save File"
Save.ShowDialog(Me)
Try
myStreamWriter = System.IO.File.AppendText(Save.FileName)
myStreamWriter.Write(txtTextPad.Text)
myStreamWriter.Flush()
Catch ex As Exception
End Try
Save As
SaveFileDialog1.ShowDialog()
' the application will check if the file is already exists,
if exists, it will ask the user if they want to replace it
If My.Computer.FileSystem.FileExists(SaveFileDialog1.FileName)
Then
Dim ask As MsgBoxResult
ask = MsgBox("File already exists,
would you like to replace it?", MsgBoxStyle.YesNo,
"File Exists")
'if the user decides not to replace the existing file
If ask = MsgBoxResult.No
Then
SaveFileDialog1.ShowDialog()
'if the user decides to replace the existing
file
ElseIf ask = MsgBoxResult.Yes
Then
My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName,
txtTextPad.Text, False)
End If
'if the file doesn't exist
Else
My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName,
txtTextPad.Text, False)
End If
Print Preview
If txtTextPad.Text = ""
Then
MessageBox.Show("Empty
is not allowed print out")
Else
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
End If
Print
If PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
PrintDocument1.Print()
End If
Exit
If MessageBox.Show("Are you really want to exit to close the
program?", "Closed", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
= Windows.Forms.DialogResult.Yes Then
Me.Close()
End If
Cut, Copy, Paste,
Undo, Redo, Select All, and Date and Time
Private Sub
UndoToolStripMenuItem_Click(sender As System.Object, e As
System.EventArgs) Handles
UndoToolStripMenuItem.Click
txtTextPad.Undo()
End Sub
Private Sub
CopyToolStripMenuItem_Click(sender As System.Object, e As
System.EventArgs) Handles
CopyToolStripMenuItem.Click
txtTextPad.Cut()
End Sub
Private Sub
PasteToolStripMenuItem_Click(sender As System.Object, e As
System.EventArgs) Handles
PasteToolStripMenuItem.Click
txtTextPad.Copy()
End Sub
Private Sub
PasteToolStripMenuItem1_Click(sender As System.Object, e As
System.EventArgs) Handles
PasteToolStripMenuItem1.Click
txtTextPad.Paste()
End Sub
Private Sub
SelectToolStripMenuItem_Click(sender As System.Object, e As
System.EventArgs) Handles
SelectToolStripMenuItem.Click
txtTextPad.SelectAll()
End Sub
Private Sub
RedoToolStripMenuItem_Click(sender As System.Object, e As
System.EventArgs) Handles
RedoToolStripMenuItem.Click
txtTextPad.redo()
End Sub
Private Sub
TimeAndDateToolStripMenuItem_Click(sender As
System.Object, e As
System.EventArgs) Handles
TimeAndDateToolStripMenuItem.Click
Dim h As String = My.Computer.Clock.LocalTime.Hour
Dim m As String = My.Computer.Clock.LocalTime.Minute
Dim d As String = My.Computer.Clock.LocalTime.Date
If m < 10 Then m =
"0" + m
If h > 12 Then h =
h - 12
Dim time As String = h + ":"
+ m
If h > 12 Then
time = time + "AM"
If h < 12 Then
time = time + "PM"
txtTextPad.Text = txtTextPad.Text + "
" + time + ", " + d
End Sub
Font
FontDialog1.Font = txtTextPad.Font
FontDialog1.ShowDialog()
txtTextPad.Font = FontDialog1.Font
Color
Dim colorDialog As New ColorDialog
Try
colorDialog.ShowDialog()
txtTextPad.ForeColor = colorDialog.Color
Catch ex As Exception
End Try
Find
Dim x As String
Dim y As String
x =
InputBox("Enter text to be found")
y =
InStr(txtTextPad.Text, x)
If y Then
txtTextPad.Focus()
txtTextPad.SelectionStart = y - 1
txtTextPad.SelectionLength = Len(x)
Else
MessageBox.Show("Text
not found!")
End If
Status Bar On and
Off
StatusStrip1.Visible = True
StatusStrip1.Visible = False
Accept Tab
If txtTextPad.AcceptsTab = True
Then
txtTextPad.AcceptsTab = False
Else
txtTextPad.AcceptsTab = True
End If
Recognize HTML
If txtTextPad.DetectUrls = True
Then
txtTextPad.DetectUrls = False
RecognizeToolStripMenuItem.Checked = False
Else
txtTextPad.DetectUrls = True
RecognizeToolStripMenuItem.Checked = True
End If
About
MessageBox.Show("The
features of this notepad the same in usual notepad can be found in your
computer, a simple notepad program created in Visual Basic.Net 2010 with my own
version of customization and function. " & vbNewLine &
vbNewLine & "By: Tesear.Com", "About", MessageBoxButtons.OK,
MessageBoxIcon.Information)
Help
MessageBox.Show("Simply
easy, almost the same function as notepad", "Help", MessageBoxButtons.OK,
MessageBoxIcon.Information)
Double click the textbox and put this code
Dim split() As String = txtTextPad.Text.Split(" ")
'count the number of characters
'it will display on your status strip
statusstripnumberoofchar.Text = "Characters:
" & txtTextPad.Text.Count()
'count the number of words
statustripnumberofwords.Text = "Word: " & split.Length
Shortcut command can be found in
your properties window.