Simple Text Editor

Text editing is one of the most common tasks performed on a computer, so here is a simple Text Editor that will introduce you to the TextBox, Open/SaveFileDialog and StreamReader/Writer!

www.cespage.com/vb/vbextut5.html

Step 1

Start Microsoft Visual Basic 2005 Express Edition, then select File then New Project... Choose Windows Application from the New Project Window, enter a name for the Project and then click OK, see below:

New Project

Step 2

A Blank Form named Form1 should then appear, see below:

Form1

Step 3

Then from the Windows Forms components tab select the Textbox control:

Textbox Component

Step 4

Draw a TextBox on the Form, see below:

Form1 with Textbox

Step 5

Then go to the Properties box change ScrollBars to Vertical and MultiLine to True, see below:

TextBox Scrollbar and Multiline Properties

Step 6

Still in the Properties box and change Size to 270,200, see below:

TextBox Size Property

The Textbox should then appear on the Form as below:

Form1 with Large Textbox

Step 7

Then from the Windows Forms components tab select the Button control:

Button Component

Step 8

Draw three Buttons on the Form as shown below:

Form1 with three Buttons

Step 9

Click on Button1 and then go to the Properties box and change the Text Property to Open, see below:

Button1 Property

Step 10

Click on Button2, change its Text Property to Save, click on Button3 and change its Text Property to Exit until the buttons appear as below:

Form1 with Open, Save and Exit Buttons

Step 11

Double Click on the Button Labeled Open (Button1) and type the folllowing in the Button1_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.Title = "Open"
Open.ShowDialog(Me)
Try
  Open.OpenFile()
  myStreamReader = System.IO.File.OpenText(Open.FileName)
  TextBox1.Text = myStreamReader.ReadToEnd()
Catch ex As Exception
  ' Do nothing on Exception
End Try

See Below:

Button1_Click Event

Step 12

Double Click on the Button Labeled Save (Button2) and type in the Button2_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.Title = "Save"
Save.ShowDialog(Me
Try
  myStreamWriter = System.IO.File.AppendText(Save.FileName)
  myStreamWriter.Write(TextBox1.Text)
  myStreamWriter.Flush()
Catch ex As Exception
  ' Do nothing on Exception
End Try

See Below:

Button2_Click Event

Step 13

Double Click on the Button Labeled Exit (Button3) and type in the Button3_Click() Sub:

End

See Below:

Button3_Click Event

Step 14

Save the Project as you have now finished the application, then click on Start:

Start

When you do the following will appear:

Application Running

Step 15

Click on Open, select a text file on your computer and open it with the open button, your file should appear in the textbox, see below:

Text Editor Application

Step 16

You can save this as a new filename or clear the Textbox and enter your own text and Save it too - just click on the Save button and Save on the dialog!

Click on the Close button Close on the top right of Form1 to end the application or click on the Exit button you created.

That was simple wasn't it? It can Load and Save Text Files! Try changing other parts of the code and extend it, you can learn a lot from this simple text editor.