Simple Text Editor

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.

www.cespage.com/vb/vbtut5.html

Step 1

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

New Project

Step 2

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

Form 1

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 as shown below:

Form with Text Box

Step 5

Then goto the Properties box and change MultiLine to True and ScrollBar to 2 - Vertical, see below:

Text Box Properties - MultiLine and ScrollBar

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 as shown below:

Form with Text Box and Three Command Buttons

Step 8

Change the Caption of Command3 to Quit using the Properties, see below:

Command Button 3 Properties

Step 9

Change the Caption of Command2 to Save and the Caption of Command1 to Load, using the Properties for each Button, so as below:

Form with Text Box and Buttons with Captions

Step 10

Double Click on the Button Labeled Load (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 Button Labeled Save (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 Button Labeled Quit (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 or Press F5 and the below will appear:

Application 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), see below:

Text Editor Open

Step 15

Click Ok and the document should be displayed in the Text Box, see below:

Text Editor File Open

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 (Load) your old File. To Quit the Application, Click on Quit.

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 text editor, try and see if you can modify this example to use the Common Dialog box!