One of the most common tasks performed is Text Editing, so this tutorial will show you how to create your own using the TextBox, Open/Save Dialogs and more! Part One of this tutorial will show you how to get a basic text editor up and running, in the next two parts you'll add more features.
Printer Friendly Download Tutorial (193KB) Download Source Code (12.4KB)
Step 1
Start Microsoft Visual Basic 2008 Express Edition, then select File then New Project... Choose Windows Forms Application from the New Project Window, enter a name for the Project and then click OK, see below:
Step 2
A Blank Form named Form1 should then appear, see below:
Step 3
With Form1 selected goto the Properties box and change the Name from Form1 to frmMain
Step 4
Then from the Common Controls tab on the Toolbox select the Textbox component:
Step 5
Draw a Textbox on the Form, see below:
Step 6
Then goto the Properties box and change the Name to txtEditor, Dock to Fill, Multiline to True and Scrollbars to Vertical, see Below:
Step 7
Then from the Menus & Toolbars tab on the Toolbox select the MenuStrip Control, see Below:
Step 8
Either double-click on the MenuStrip Control entry in the Menu & Toolbars tab on the Toolbox or keep the MenuStrip Component Clicked and then move it over the Form then let go. Change the Name of the MenuStrip in the properties to mnuText The Form will then look as below:
Step 9
Click or select where the MenuStrip says "Type Here", an editable Textbox will appear type in "File" without the quotes in this Box, then in the Box below that type in "New", below that "Open...", then "Save..." and finally "Exit". The MenuStrip should look as below:
Step 10
Click on "File" on the MenuStrip to show the MenuStrip then Double Click on the Menu Item Labeled "New" (NewToolStripMenuItem) and type the following in the NewToolStripMenuItem_Click() Sub:
Dim Response As MsgBoxResult
Response = MsgBox("Are you sure you want to start a New Document?", _
MsgBoxStyle.Question + MsgBoxStyle.YesNo, _
"Text Editor")
If Response = MsgBoxResult.Yes Then
txtEditor.Text = ""
Me.Text = "Text Editor - Untitled"
End If
See Below:
Step 11
Click on the [Design] Tab to view the form again, then Click on "File" on the MenuStrip to show the MenuStrip then Double Click on the Menu Item Labeled "Open..." (OpenToolStripMenuItem) and type the following in the OpenToolStripMenuItem_Click() Sub:
Dim Open As New OpenFileDialog() Dim myStreamReader As System.IO.StreamReader Open.Filter = "Plain Text Files (*.txt)|*.txt|All files (*.*)|*.*" Open.CheckFileExists = True Open.ShowDialog(Me) Try Open.OpenFile() myStreamReader = System.IO.File.OpenText(Open.FileName) txtEditor.Text = myStreamReader.ReadToEnd() Me.Text = "Text Editor - " & Open.FileName Catch ex As Exception ' Do nothing on Exception End Try
See Below:
Step 12
Click on the [Design] Tab to view the form again, then Click on "File" on the MenuStrip to show the MenuStrip then Double Click on the Menu Item Labeled "Save..." (SaveToolStripMenuItem) and type the following in the SaveToolStripMenuItem_Click() Sub
Dim Save As New SaveFileDialog() Dim myStreamWriter As System.IO.StreamWriter Save.Filter = "Plain Text Files (*.txt)|*.txt|All files (*.*)|*.*" Save.CheckPathExists = True Save.ShowDialog(Me) Try myStreamWriter = System.IO.File.AppendText(Save.FileName) myStreamWriter.Write(txtEditor.Text) myStreamWriter.Flush() Me.Text = "Text Editor - " & Save.FileName Catch ex As Exception ' Do nothing on Exception End Try
See Below:
Step 13
Click on the [Design] Tab to view the form again, then Click on "File" on the MenuStrip to show the MenuStrip then Double Click on the Menu Item Labeled "Exit" (ExitToolStripMenuItem) and type the following in the ExitToolStripMenuItem_Click() Sub
Dim Response As MsgBoxResult
Response = MsgBox(Are you sure you want to Exit Text Editor?", _
MsgBoxStyle.Question + MsgBoxStyle.YesNo, _
"Text Editor")
If Response = MsgBoxResult.Yes Then
End
End If
See Below:
Step 14
Click on the [Design] Tab to view the form again, then Double Click on the Form (frmMain) and type the following in the frmMain_Load() Sub
txtEditor.Text = "" Me.Text = "Text Editor - Untitled"
See Below:
Step 15
Save the Project as you have now finished the application, then click on Start:
When you do the following will appear:
Step 16
Click on the File Menu and select Open...,this will show the Open File Dialog, then select a Text File on your Computer this should appear in the Textbox, see below:
Step 17
Click File then Exit or click on the Close button
on the top right of the Text Editor to end the application.
This Text Editor has some of the basic features needed by any text editor and is a whole application in itself, however you can add even more, try adding more features yourself such, or wait for Part Two where the main Edit and Format features will be added!

