.Open Visual Basic 2008 Express Edition. (If you don't have the program click here to download it for free.)
Click on Project next to Create:
Click on Windows Form Application, and name the project "Your name Application" and then click Ok.
You can change the form text to "your name's notepad". Click on the form and then go to properties window, then change the text property.
Add a textbox from the Control box, and change it's property to multi line. To change the property click on the text box, and then click on the little arrow on the top right of the text box. You will see a new box, check MuliLine.
Once you check MultiLine, you will be able to resize the textbox, and therefore change the textbox size to fit the form from the left, right and bottom like this:
Change the text box anchor property to Top, Bottom, Left, Right. To do so, click on the text box, go to properties window, change the anchor property:
Click on the boxes that appear in gray as the image above.
This property will make the text box resize as the form resizes. For example, if the user want to maximize the form, the text box's size will also maximizes.
Add a MenuStrip from the control box, and place on top of the form between the form's control box, and the textbox.
Add File, Edit, Format, Help to the MenuStrip as follows:
Under File add New, Open, Save, and Exit as follows:
Note: for printing and print preview please click here
Under Edit add Undo, Cut, Copy, Paste, Select All and Find
Notice that we didn't add Redo because textbox control doesn't support it.
Under Format add Font, Text Color, Text Alignment, and Back Color.
Under Help add About
We need to add invisible controls to the notepad:
Click on SaveFileDialog in the Toolbox and add to the form:
SaveFileDialog is an invisible control that appears only when it's called. However, it's icon appear in the gray area as it appears on the image above.
Right click on SaveFileDialog1 and click properties:
Change the properties as follows:
We should also add OpenFileDialog to the form.
Right click on OpenDialog1 and click properties:
Change the properties as follows:
Once your project look like the image below, let's add some codes to the File menu:
File New
Now, single click on File in the notepad you are working on, and then double click on New. The codes page will open, and you have to add the following highlighted code:
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
'Check if there's text added to the textbox
If TextBox1.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
TextBox1.Clear()
ElseIf ask = MsgBoxResult.Cancel Then
ElseIf ask = MsgBoxResult.Yes Then
SaveFileDialog1.ShowDialog()
My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text, False)
TextBox1.Clear()
End If
'If textbox's text is still the same, notepad will open a new page:
Else
TextBox1.Clear()
End If
End Sub
File Open:
After adding the above highlighted code go back to the Designer and single click on File the one inside the notepad then double click on Open:
Add the following highlighted code:
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
'Check if there's text added to the textbox
If TextBox1.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()
TextBox1.Text = My.Computer.FileSystem.ReadAllText(OpenFileDialog1.FileName)
ElseIf ask = MsgBoxResult.Cancel Then
ElseIf ask = MsgBoxResult.Yes Then
SaveFileDialog1.ShowDialog()
My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text, False)
TextBox1.Clear()
End If
Else
'If textbox's text is still the same, notepad will show the OpenFileDialog
OpenFileDialog1.ShowDialog()
TextBox1.Text = My.Computer.FileSystem.ReadAllText(OpenFileDialog1.FileName)
End If
End Sub
File Save
Go back to designer tab, and double click on Save under File in your notepad and add the following highlighted codes:
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
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, TextBox1.Text, False)
End If
'if the file doesn't exist
Else
My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text, False)
End If
End Sub
File Exit
Go back to designer page and double click on Exit under File menu in your notepad and add the following highlighted code:
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
'exit the program
End
End Sub
Edit Undo
Under Edit, double click on Undo and add the following highlighted code:
Private Sub UndoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UndoToolStripMenuItem.Click
'check if textbox can undo
If TextBox1.CanUndo Then
TextBox1.Undo()
Else
End If
End Sub
Edit Cut
Double click on Cut and add the following highlighted code:
Private Sub CutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CutToolStripMenuItem.Click
My.Computer.Clipboard.Clear()
If TextBox1.SelectionLength > 0 Then
My.Computer.Clipboard.SetText(TextBox1.SelectedText)
End If
TextBox1.SelectedText = ""
End Sub
Edit Copy
Double click on Copy and add the following highlighted code:
Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click
My.Computer.Clipboard.Clear()
If TextBox1.SelectionLength > 0 Then
My.Computer.Clipboard.SetText(TextBox1.SelectedText)
End If
End Sub
Edit Paste
Double click on Paste and add the following highlighted code:
Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteToolStripMenuItem.Click
If My.Computer.Clipboard.ContainsText Then
TextBox1.Paste()
End If
End Sub
Edit Select All
Double click on Select All and add the following highlighted code:
Private Sub SelectAllToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectAllToolStripMenuItem.Click
TextBox1.SelectAll()
End Sub
Edit Find
Double click on Find and add the following highlighted code:
Private Sub FindToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindToolStripMenuItem.Click
Dim a As String
Dim b As String
a = InputBox("Enter text to be found")
b = InStr(TextBox1.Text, a)
If b Then
TextBox1.Focus()
TextBox1.SelectionStart = b - 1
TextBox1.SelectionLength = Len(a)
Else
MsgBox("Text not found.")
End If
End Sub
Before we add the code for Format we should add FontDialog, ColorDialog from the toolbox.
After adding FontDialog your gray area should look like this:
Format Font
Single click on Format and then single click on Font. Add the following highlighted code:
Private Sub FontToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FontToolStripMenuItem.Click
FontDialog1.ShowDialog()
TextBox1.Font = FontDialog1.Font
End Sub
Format Font Color
Double click on Font Color and the following highlighted code:
Private Sub FontColorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FontColorToolStripMenuItem.Click
ColorDialog1.ShowDialog()
TextBox1.ForeColor = ColorDialog1.Color
End Sub
Format Text Alignment
Now in order to do that we should add a sub menu to Text Alignment:
Add Left, Center, Right to Text Allignment
Double click on Left and add the following highlighted code:
Private Sub LeftToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LeftToolStripMenuItem.Click
TextBox1.TextAlign = HorizontalAlignment.Left
LeftToolStripMenuItem.Checked = True
CenterToolStripMenuItem.Checked = False
RightToolStripMenuItem.Checked = False
End Sub
Double click on Center and add the following highlighted code:
Private Sub CenterToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CenterToolStripMenuItem.Click
TextBox1.TextAlign = HorizontalAlignment.Center
LeftToolStripMenuItem.Checked = False
CenterToolStripMenuItem.Checked = True
RightToolStripMenuItem.Checked = False
End Sub
Double Click on Right and add the following highlighted code:
Private Sub RightToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RightToolStripMenuItem.Click
TextBox1.TextAlign = HorizontalAlignment.Right
LeftToolStripMenuItem.Checked = False
CenterToolStripMenuItem.Checked = False
RightToolStripMenuItem.Checked = True
End Sub
Format Back Color
Double click on Back Color and add the following highlighted code:
Private Sub BackColorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BackColorToolStripMenuItem.Click
ColorDialog1.ShowDialog()
TextBox1.BackColor = ColorDialog1.Color
End Sub
The last part of the notepad is About
You can add whatever text you like to your notepad's about including your name and copyright:
You can do that by displaying a message box:
Double click on About and add the following the following highlighted code:
Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
MsgBox("Your Text")
End Sub
Test your application:
To publish the application:
Run your notepad and test your notepad. Your notepad should work fine if you added the right code to it.
Source :- tutorialized.com