Simple Text Editor

Learn to make a Simple Text Editor using Visual Basic 6 with this Tutorial that Celebrates the CESPage 25th Anniversary

Text Editing is one of the most common tasks performed on a computer, so here is a simple one which will introduce you to the Text Box Control plus a little about Input Boxes and using Files.

Step 1

Load Microsoft Visual Basic, then select Standard EXE Project if VB5/6, click on the option then click Open
New Project

Step 2

A Blank Form named Form1 should then appear
Blank Form

Step 3

Then from the Visual Basic Components Menu Select the Text Box Control:
Text Box Control

Step 4

Draw a Text Box on the Form
Form with Text Box

Step 5

Then goto the Properties Box and change MultiLine to True and ScrollBar to 2 - Vertical
Text Box MultiLine and ScrollBars Property

Step 6

Then from the Visual Basic Components Menu Select the Command Button Control:
Command Button Control

Step 7

Draw Three Command Buttons on the Form
Form with Three Command Buttons

Step 8

Change the Caption of Command3 to Quit using the Properties Box
Command Button Caption Property

Step 9

Change the Caption of Command2 to Save and the Caption of Command1 to Load, using the Properties Box for each Command Button
Form with Load, Save and Quit Command Buttons

Step 10

Double Click on the Command Button labelled Load or Command1 and type in the Command1_Click() Sub


Dim strName As String, strFile As String, strTemp As String
On Error GoTo ErrHandler
strName = InputBox("Filename:")
Open strName For Input As #1
    strFile = ""
    Do Until EOF(1)
        Line Input #1, strTemp
        strFile = strFile & strTemp & vbCrLf
    Loop
    Text1.Text = strFile
Close #1
ErrHandler: 
                                    

Step 11

Double Click on the Command Button labelled Save or Command2 and type in the Command2_Click() Sub


Dim strName As String
On Error GoTo ErrHandler
strName = InputBox("Filename:")
Open strName For Output As #1
    Print #1, Text1.Text
Close #1
ErrHandler:
                                    

Step 12

Double Click on the Command Button labelled Quit or Command3 and type in the Command3_Click() Sub


Unload Me
                                    

Step 13

Save the Project, for example prjTextEditor, into a vacant folder as you have finished the application. Click on Start / Run
Start / Run
This should Start the application
Simple Text Editor Running

Step 14

Now Click on Load and an Input Box will appear, type a location of a file (e.g. c:\windows\readme.txt)
Simple Text Editor Open

Step 15

Click on Ok and the Document should be displayed in the Text Box
Simple Text Editor Opened

Step 16

You can Save a txt (Text) File by Clicking on Save and enter a filename, the contents of the Text Box will be saved to that File so that you can Open or Load your old File. To Exit the application, Click on Quit or End / Stop
End / Stop

That was simple wasn't it? It can Load and Save Text Files! Try changing the program so that it checks whether a file exists for Load and Save. Try changing other parts of the code and extending it, you can learn a lot from this Simple Text Editor, try and see if you can modify this example to use the Common Dialog box!