Text Editor (Part One)

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:

New Project

Step 2

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

Form1

Step 3

With Form1 selected goto the Properties box and change the Name from Form1 to frmMain

Form1 Properties

Step 4

Then from the Common Controls tab on the Toolbox select the Textbox component:

Textbox Component

Step 5

Draw a Textbox on the Form, see below:

frmMain with Textbox

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:

TextBox Properties

Step 7

Then from the Menus & Toolbars tab on the Toolbox select the MenuStrip Control, see Below:

MenuStrip Component

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:

frmMain with MenuStrip

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:

mnuText with SubMenus

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:

File Menu New Menu Item Click Event

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:

File Menu Open Menu Item Click Event

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:

File Menu Save Menu Item Click Event

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:

File Menu Exit Menu Item Click Event

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:

frmMain Load Event

Step 15

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

Start

When you do the following will appear:

Text Editor Running

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:

Text Editor with Opened Document

Step 17

Click File then Exit or click on the Close button Close 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!

Copyright Comentsys © 1997 - 2008, All rights reserved. About | Contact | Link to Page
Valid XHTML 1.1! Valid CSS Level Double-A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0 This website is ICRA rated