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/vbdntut5.html

Step 1

Load Microsoft Visual Studio.NET then select Visual Basic Projects, Windows Application, 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 and change ScrollBars to Vertical and MultiLine to True, see below:

TextBox Properties

Step 6

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

Button Component

Step 7

Draw three Buttons on the Form as shown below:

Form1 with three Buttons

Step 8

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

Button1 Property

Step 9

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 10

Double Click on the Button Labeled Open (Button1) and type 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
Finally
  If Not myStreamReader Is Nothing Then
    myStreamReader.Close()
  End If
End Try

See Below:

Button1_Click Sub

Step 11

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
Finally
  If Not myStreamReader Is Nothing Then
    myStreamWriter.Close()
  End If
End Try

See Below:

Button2_Click Sub

Step 12

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

End

See Below:

Button3_Click Sub

Step 13

Save the Project as you have now finished the application, select Release and Click on Start:

Start (Release)

When you do the following will appear:

Application Running

Step 14

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 15

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.

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.