Now we are going to basically recreate the windows application Notepad. The interface is very simple to set up, but be prepared to type LOTS of code. This is probably the longest page in the entire tutorial! Hope you enjoy it...
Start a new project.
Name the project TextEditor.
Name the form frmEdit.
Set the form's caption to Document - Text Editor.
Press Ctrl then T. This opens the components window.
Select Microsoft Common Dialog Control 6.0 (SP3).
Press OK.
Double-click the new control on the toolbox (CommonDialog) to add it to the form.
Name the control dlgEdit.
Double-click the textbox control on the toolbox twice to add two to the form.
Name one textbox txtEdit.
Position txtEdit in the upper-left corner of the form.
Set the text box's ScrollBars property to 3-Both and its MultiLine property to True.
Name the other text box txtEditWrap.
Set the text box's ScrollBars property to 2-Vertical and its MultiLine property to True.
Position txtEditWrap in the upper-left corner of the screen, the same place as the other textbox.
Set the Visible property of txtEditWrap to False.
Select the form, then click on the menu icon on the standard toolbar.
Type &File in the caption text box, and type mnuFile in the name text box.
Press Next, then the right arrow.
Type &New in the caption text box, and type mnuFileNew in the name text box.
Select Ctrl-N in the Shortcut combo box.
Press Next.
Type &Open... in the caption text box, and type mnuFileOpen in the name text box.
Select Ctrl-O in the Shortcut combo box.
Press Next.
Type a hyphen in the caption text box, and type Hyphen1 in the name text box.
Press Next.
Type &Save in the caption text box, and type mnuFileSave in the name text box.
Select Ctrl-S in the Shortcut combo box.
Press Next.
Type &Save As... in the caption text box, and type mnuFileSaveAs in the name text box.
Select Ctrl-A in the Shortcut combo box.
Press Next.
Type a hyphen in the caption text box, and type Hyphen2 in the name text box.
Press Next.
Type &Print in the caption text box, and type mnuFilePrint in the name text box.
Select Ctrl-P in the Shortcut combo box.
Press Next.
Type a hyphen in the caption text box, and type Hyphen3 in the name text box.
Press Next.
Type &Exit in the caption text box, and type mnuFileExit in the name text box.
Press Next.
Press the left arrow.
Set up the menu as seen below using the same process.
The grayed menu items are disabled. Simply uncheck the Enabled checkbox to disable them.
![]() |
|
Line 1: Sets the height of the text box txtEdit to
move with the size of the form frmEdit.
Line 2: Sets the width of the text box txtEdit to move with the
size of the form frmEdit.
Lines 3 and 4: Does the same as lines 2 and 3, but with the text box txtEditWrap.
Select mnuEditPaste in the object box of the code window.
Press tab, then type the following lines:
If txtEdit.Visible = True Then
txtEdit.SelText = Clipboard.GetText()
Else: txtEditWrap.SelText = Clipboard.GetText
End If
Line 1: Test to see if the text box txtEdit is
visible.
Line 2: Set the selected text of the text box txtEdit to the text
in the clipboard. The clipboard is a windows object, used to store the text a
user cuts or copies.
Line 3: Otherwise (txtEdit is not visible, therefore txtEditWrap
is visible), set selected text in the text box txtEditWrap to the text in
the clipboard.
Line 4: End the If statement.
Select mnuEditCopy in the object box of the code window.
Press tab, then type the following lines:
Clipboard.Clear
If txtEdit.Visible = True Then
Clipboard.SetText txtEdit.SelText
Else: Clipboard.SetText txtEditWrap.SelText
End If
Line 1: Clear the contents of the clipboard.
Line 2: Test to see if the text box txtEdit is visible.
Line 3: Set the text in the Clipboard to the selected text (SelText
property of txtEdit) of txtEdit.
Line 4: Otherwise (txtEdit is not visible, therefore txtEditWrap
is visible), set the text in the Clipboard to the selected text in txtEditWrap.
Line 5: End the If statement.
Select mnuEditCut in the object box of the code window.
Press tab, then type the following lines:
Clipboard.Clear
If txtEdit.Visible = True Then
Clipboard.SetText txtEdit.SelText
txtEdit.SelText = ""
Else
Clipboard.SetText txtEditWrap.SelText
txtEditWrap.SelText = ""
End If
Line 1: Clear the contents of the clipboard.
Line 2: Test to see if the text box txtEdit is visible.
Line 3: Set the text in the Clipboard to the selected text (SelText
property of txtEdit) of txtEdit.
Line 4: Clear the selected text in the text box txtEdit.
Line 5: Otherwise, do below.
Line 6: Set the selected text of txtEditWrap to the Clipboard.
Line 7: Clear the selected text in txtEditWrap.
Line 8: End the If statement.
Select mnuEditDelete in the object box of the code window.
Press tab, then type the following lines:
If txtEdit.Visible = True Then
txtEdit.SelText = ""
Else: txtEditWrap.SelText = ""
End If
Line 1: Test to see if the text box txtEdit is
visible.
Line 2: Clear the selected text in txtEdit.
Line 3: Clear the selected text in txtEditWrap when txtEdit
is NOT visible.
Line 4: End the If statement.
Select mnuEdit in the object box of the code window.
Press tab, then type the following lines:
If txtEdit.SelText = "" And txtEditWrap.SelText = "" Then
mnuEditCut.Enabled = False
mnuEditCopy.Enabled = False
Else
mnuEditCut.Enabled = True
mnuEditCopy.Enabled = True
End If
If Clipboard.GetText() = "" Then
mnuEditPaste.Enabled = False
Else: mnuEditPaste.Enabled = True
End If
Line 1: Test to see if there is no text selected in txtEdit and
txtEditWrap
Line 2: Disable mnuEditCut when no text is selected.
Line 3: Disable mnuEditCopy when no text is selected.
Line 4: Otherwise (one of the text boxes has test selected).
Line 5: Enable menu item mnuEditCut.
Line 6: Enable menu item mnuEditCopy.
Line 7: End the If statement.
Line 8: Get the text from the clipboard; when there is no text in the
clipboard, do below.
Line 9: Disable menu item mnuEditPaste when no text is in the
clipboard.
Line 10: Otherwise (there is some text in the clipboard), enable menu
item mnuEditPaste.
Line 11: End the If statement.
Option Explicit
Dim NewForm As New frmEdit
Dim strSearchFor As String
Dim blnLoaded As Boolean
Dim blnChange As Boolean, blnCancelSave As Boolean, blnWrap As Boolean
Line 1: Tells Visual Basic to display an error if any procedure uses
an undeclared variable
Line 2: Creates the variable NewForm as a new frmEdit
object. This will allow more than one form to run at the same time, just as Notepad
does.
Line 3: Create variable strSearchFor as the String
data-type. This is used to store what the user wants to search for in the
editor.
Line 4: Create variable blnLoaded as the Boolean data-type.
This is used to tell the program that the program is already running in order to
determine when to create a new form. This will become more clear later.
Line 5: Create variable blnChange as the Boolean data-type.
This is used as a flag to determine if the current document has been changed.
Create variable blnCancelSave as the Boolean data-type. This is
used to handle the CancelError on the common Save As dialog.
Create the variable blnWrap as the Boolean data-type. This is a
flag used to determine whether or not text wrap is used.
If blnLoaded = True Then NewForm.Show
blnLoaded = True
blnChange = False
blnCancelSave = False
Line 1: Test to see if the variable blnLoaded is True.
If it is, display NewForm (which is declared as an object, specifically
the form we are using). Just like Notepad, this allows multiple windows of the
same application to be open.
Line 2: Set the variable blnLoaded to True so that the next
time the Form_Load procedure executes, line one knows that it is loaded.
Lines 3 and 4 set the value of blnChange and blnCancelSave
initially. It is a good habit to set certain form-level variables to a value in
the form's Load event.
If mnuEditWrap.Checked = False Then
mnuEditWrap.Checked = True
txtEditWrap.Text = txtEdit.Text
txtEdit.Text = ""
txtEdit.Visible = False
txtEditWrap.Visible = True
txtEditWrap.SelStart = Len(txtEditWrap.Text)
Exit Sub
End If
If mnuEditWrap.Checked = True Then
mnuEditWrap.Checked = False
txtEdit.Text = txtEditWrap.Text
txtEditWrap.Text = ""
txtEdit.Visible = True
txtEditWrap.Visible = False
txtEdit.SelStart = Len(txtEdit.Text)
End If
Line 1: Test to see in the Edit menu item is checked; when it
is not (or when Checked = False; item is not checked), do below.
Line 2: Check the menu item mnuEditWrap.
Line 3: Set the text in the text box txtEditWrap to the text in
the text box txtEdit.
Line 4: Clear the text in the text box txtEdit.
Line 5: Hide the text box txtEdit.
Line 6: Display the text box txtEditWrap.
Line 7: Set the position of the cursor to the end of txtEditWrap.
The SelStart property tells it where to begin. We take the length (Len)
of the text in the text box txtEditWrap because this returns a number.
The number is set to the SelStart property of the text box. Suppose the
length was 52. The code would then set the SelStart property of txtEditWrap
to 52, which is the end of the text, because that is the Length of
the text.
Line 8: Exit the sub-procedure. In Line 2, the menu item was
checked. Line 10 tests to see if the menu item is checked, and if it is,
it executes the code below it. Since it is checked, it continues to execute the
code. The Exit Sub prevents this.
You know Line 9.
Line 10: Test to see if the menu item mnuEditWrap is checked.
Line 11: Uncheck the menu item mnuEditWrap.
Line 12: Set the text of txtEdit to the the text in txtEditWrap.
Line 13: Clear the text in the text box txtEditWrap.
Line 14: Display the text box txtEdit.
Line 15: Hide the text box txtEditWrap.
Line 16: Set the position of the cursor to the end of txtEdit.
This works the same way as on Line 7.
You know Line 17.
Unload frmEdit
You know what this means by now, so no explaining is necessary--I hope!
Const conBtns As Integer = vbYesNoCancel + vbExclamation
Const conMsg As String = "Do you want to save the current document?"
Dim intUserResponse As Integer
On Error GoTo OpenErrHandler
dlgEdit.CancelError = True
If blnChange = True Then 'document was changed since last save
intUserResponse = MsgBox(conMsg, conBtns, "Editor")
If intUserResponse = vbYes then
Call mnuFileSave_Click
If blnCancelSave = True Then Exit Sub
End If
End If
dlgEdit.Filter = "Text Files(*.txt) | *.txt | HTML Files(*.htm) | *.htm |
All Files(*.*) | *.*"
dlgEdit.FileName = ""
dlgEdit.ShowOpen
Open dlgEdit.FileName For Input As #1
If txtEdit.Visible = True Then
txtEdit.Text = Input(LOF(1), 1)
Else
txtEditWrap.Text = Input(LOF(1), 1)
End If
Close #1
blnChange = False
frmEdit.Caption = dlgEdit.FileName & " - Text Editor"
Exit Sub
OpenErrHandler:
Line 1: Declare constant conBtns as an integer and include the Yes,
No, and Cancel buttons on the Exclamation dialog.
Line 2: Declare constant conMsg as a string to store a common
message for each message.
Line 3: Declare variable intUserResponse as an Integer data
type to store the button the user clicks on in the message box.
Line 4: When an error occurs, go to the last line in the code, thus doing
nothing.
Line 5: Set CancelError in the common dialog dlgEdit to True.
Line 6: Check to see if something has changed in the document.
Line 7: When something has changed, display a message box, using the text
from the constant conMsg and buttons from conBtns, and Editor
as the title in the message.
Line 8: Test to see If the user clicked Yes.
Line 9: Call the mnuFileSave_Click procedure, therefore executing
the code within it.
Line 10: Exit the sub-procedure if the user clicks Cancel.
Line 11: End the If statement within the first If statement.
Line 12: End the first If statement--the one including the other If
statement.
Line 13: Set the filter of the common dialog to text files, HTML
files, and All files.
Line 14: Set the File name in the dialog to a blank so it appears
blank when the dialog is displayed.
Line 15: Display the Open dialog.
Line 16: Open the file the user selects in the Open dialog to read
it (For Input) as file number one.
Line 17: Test to see which text box is visible; the one which wraps the
text, or the one which scrolls horizontally.
Line 18: Set the text of the text box txtEdit to the data in file
number one.
Line 20: Set the text of the text box txtEditWrap to the data in
file number one.
Line 22: Close file number one.
Line 23: Set the variable blnChange to False so that the
editor does not think the file has been changed.
Line 24: Set the caption of the form frmEdit to the name of the
open file and (&) - Text Editor.
The last lines of the code should be self-explanatory.
dlgEdit.Flags = cdlCFScreenFonts
dlgEdit.FontName = txtEdit.FontName
dlgEdit.FontBold = txtEdit.FontBold
dlgEdit.FontItalic = txtEdit.FontItalic
dlgEdit.FontSize = txtEdit.FontSize
dlgEdit.ShowFont
txtEdit.FontName = dlgEdit.FontName
txtEdit.FontBold = dlgEdit.FontBold
txtEdit.FontItalic = dlgEdit.FontItalic
txtEdit.FontSize = dlgEdit.FontSize
txtEditWrap.FontName = dlgEdit.FontName
txtEditWrap.FontBold = dlgEdit.FontBold
txtEditWrap.FontItalic = dlgEdit.FontItalic
txtEditWrap.FontSize = dlgEdit.FontSize
Lines 1 - 5: Set the appropriate properties of the common dialog.
Lines 2 - 5 take the properties of the text box txtEdit and set
them to the appropriate properties of the common dialog so that the dialog knows
what is currently on the text box.
Line 6: Display the common font dialog.
Lines 7 - 14: Set the appropriate properties of the text boxes on the
form to what the user selects in the common dialog.
If txtEdit.Visible = True Then
txtEdit.SelStart = 0
txtEdit.SelLength = Len(txtEdit.Text)
Else
txtEditWrap.SelStart = 0
txtEditWrap.SelLength = Len(txtEditWrap.Text)
End If
Line 1: Test to see if the text box txtEdit is visible so that
the program knows which text box to select all the text in.
Line 2: Begin selection at position 0 in the txtEdit text
box.
Line 3: Select all the text in the text box txtEdit. The SelLength
property sets this. The Len function tells the program how long the text
is, and it selects all the text because Len returns the TOTAL amount of
characters in the text box.
Line 5: When the text box txtEditWrap is visible, begin selection
at position 0 in the txtEditWrap text box.
Line 6: Select all the text in the txtEditWrap text box.
On Error GoTo PrintErrorHandler
dlgEdit.Flags = cdlPDNoSelection + cdlPDHidePrintToFile + cdlPDNoPageNums
dlgEdit.CancelError = True
dlgEdit.ShowPrinter
PrintForm
Exit Sub
PrintErrorHandler:
Line 1: Tell the code to go to the label on Line 7 when an
error occurs -- when the user presses Cancel.
Line 2: Set the appropriate properties of the common dialog. cdlPDNoSelection
disables the Selection option button on the print dialog; cdlPDHidePrintToFile
keeps the Print to File checkbox on the print dialog hidden; cdlPDNoPageNums
disables the text boxes which accept specific page numbers to print. cdlPD
simply means Common Dialog Print Dialog.
Line 3: Set CancelError to True.
Line 4: Display the Print common dialog.
Line 5: Print the form.
Line 6: Exit the sub-procedure.
Line 7: Set the label for Line 1 to go to when an error occurs in
the code between those lines.
If frmEdit.Caption = "Document - Text Editor" Then
Call mnuFileSaveAs_Click
Else
If txtEdit.Visible = True Then
Open dlgEdit.FileName For Output As #1
Print #1, txtEdit.Text
Close #1
blnChange = False
End If
If txtEditWrap.Visible = True Then
Open dlgEdit.FileName For Output As #1
Print #1, txtEditWrap.Text
Close #1
blnChange = False
End If
End If
Line 1: Test to see if the caption of the form is still in its startup
form. Recall that when the user clicks Open on the File menu, the
caption displays the path of the file. When the file has not been saved, it
brings up the Save As dialog. Otherwise, the code saves the application
without opening any dialog.
Line 2: Call the sub-procedure mnuFileSaveAs_Click, therefore
executing the code within it.
Line 3: Otherwise, execute the code below.
Line 4: Test to see if the text box txtEdit is visible.
Line 5: When the text box txtEdit is visible, open the file
entered into the common dialog to write over it (Output) as file number 1.
Line 6: Print the text in the text box txtEdit to file number one.
Line 7: Close the file.
Line 8: Change the variable which determines whether or not a change has
occurred (bolChange) to False.
Line 9 is obvious.
Line 10: Test to see if the text box txtEditWrap is visible or
not.
Lines 11 through 16 do the same as lines 5 through 8, but with txtEditWrap.
On Error GoTo SaveErrHandler
dlgEdit.CancelError = True
dlgEdit.Flags = cdlOFNOverwritePrompt + cdlOFNPathMustExist
dlgEdit.Filter = "Text Files(*.txt)|*.txt|HTML Files(*.htm)|*.html"
dlgEdit.ShowSave
If txtEdit.Visible = True Then
Open dlgEdit.FileName For Output As #1
Print #1, txtEdit.Text
Close #1
Else
Open dlgEdit.FileName For Output As #1
Print #1, txtEditWrap.Text
Close #1
End If
frmEdit.Caption = dlgEdit.FileName & " - Text Editor"
blnChange = False
blnCancelSave = False
Exit Sub
SaveErrHandler:
blnCancelSave = True
Line 1: Turn on error handling for sub-procedure, and go to the label
at the end of the code when an error occurs.
Line 2: Treat the cancel button on the common Edit dialog as an
error.
Line 3: Set the properties of the common dialog. Prompt to see if the
user wants to overwrite the file if they choose the same file name.
Line 4: Set the filter of the common dialog to Text files and HTML
files.
Line 5: Display the common Save dialog.
Line 6: Test to see which text box is visible.
Line 7: When the text box txtEdit is visible, open the file the
user selects in the common dialog dlgEdit as file number 1.
Line 8: Print (send, write) the text in txtEdit to file number
one.
Line 9: Close file number one.
Line 10: Otherwise (txtEditWrap is visible), do below.
Line 11: Same as line 7.
Line 12: Print the text in the text box txtEditWrap to file number
one.
Line 13: Close file number one.
Line 14: End the If statement.
Line 15: Set the caption of the form frmEdit to the file name of
the opened file, and - Text Editor.
Line 16: Set the value of the variable blnChange to False
so that the program does not detect changes because there are none anymore now
that the file has been saved.
Line 17: Reset the variable blnCancelSave to False.
Line 18: Exit the sub-procedure so that the lines below are not executed.
Line 19: Set the label for the Save error, which the code is set
to go to on Line 1.
Line 20: Sets the variable blnCancelSave to True to tell
the program that an error has occurred and store it so that the program does not
end. An error occurs in this code when the user selects cancel because the code
has no file to deal with, but it tries to. For instance, when the code gets to Line
7, it tries to open a file, regardless of what the user does. This creates
an error, but the first line tells the code to go to the label SaveErrHandler
on line 19. Therefore, the code is skipped over, thus avoiding an error.
Now we are going to code the Unload event for the form.
Const conBtns As Integer = vbYesNoCancel + vbExclamation
Const conMsg As String = "Do you want to save the current document?"
Dim intUserResponse As Integer
If blnChange = True Then 'document was changed since last save
intUserResponse = MsgBox(conMsg, conBtns, "Editor")
Select Case intUserResponse
Case vbYes 'user wants to save current document
Call mnuFileSave_Click
If blnCancelSave = True Then 'user canceled save
Cancel = 1 'return to document-don't unload form
End If
Case vbCancel
Cancel = 1 'return to document-don't unload form
End Select
End If
Line 1: Declare constant conBtns as an integer to
store a Yes, No, and Cancel button, plus the Exclamation
message format.
Line 2: Declare constant conMsg as a string to store the
text you see typed there.
Line 3: Declare the variable intUserResponse as an integer
to store what the user pressed in the message box.
Line 4: Test the variable blnChange.
Line 5: Prompt the user to ask them if they wish to save the document.
Line 6: Begin Select Case for the data in the variable intUserResponse.
Line 7: When intUserResponse returns (is equal to) vbYes
-->
Line 8: Call the sub-procedure mnuFileSave_Click.
Lines 9 through 11 self-explanatory from comments.
Line 12: When the user selects cancel, execute line 13.
Line 13: You noticed in the line that defines the sub-procedure that it
says Cancel as Integer. This specifies a parameter, or argument.
The rest should be self-explanatory.
This line is necessary to tell the other procedures that a change has occurred in the file in order to prompt to save the file.
If txtEditWrap.SelLength > 0 Then
mnuEditDelete.Enabled = True
Else: mnuEditDelete.Enabled = False
End If
Line 1: Test to see if the length of the selected text in the text box
txtEditWrap is greater than 0.
Line 2: When line 1 is true (text is highlighted), enable the menu
item mnuEditDelete.
Line 3: Otherwise (length of the selected text in the text box txtEditWrap
is 0), disable the menu item mnuEditDelete.
Need help? See below:
The first line should read: If txtEdit.SelLength > 0 Then
Const conBtns As Integer = vbYesNoCancel + vbExclamation
Const conMsg As String = "Do you want to save the current document?"
Dim intUserResponse As Integer
If blnChange = True Then 'text box was changed since last save
intUserResponse = MsgBox(conMsg, conBtns, "Editor")
If intUserResponse = vbYes then
Call mnuFileSave_Click
If blnCancelSave = True Then Exit Sub
End If
End If
If txtEdit.Visible = True Then txtEdit.Text = "" Else txtEditWrap.Text = ""
blnChange = False 'reset variable
frmEdit.Caption = "Document - Text Editor"
dlgEdit.FileName = ""
Line 1: Declare constant conBtns as an Integer to
include the Yes, No, and Cancel buttons as an exclamation
message type.
Line 2: Declare constant conMsg as a String and store the
text Do you want to save the current document?.
Line 3: Declare variable intUserResponse as an Integer,
which will store the button the user presses.
Line 4: Test to see if there is a change to the document.
Line 5: Prompt the user If there is a change to the document,
asking them if they would like to save the document.
Line 6: Test the response of the user.
Line 7: When the user clicks Yes, call the sub-procedure mnuFileSave_Click,
which executes that procedures code.
Line 8: Test to see if the user pressed the Cancel button; If
they did, Exit the sub-procedure (Exit Sub).
Line 9: End the embedded If statement.
Line 11: Test to see if the text box txtEdit is visible. When it
is, clear the text in txtEdit. When it is not (which means that the text
box txtEditWrap must be because one of the text boxes are always
visible), clear the text in txtEditWrap.
Line 12: Reset the variable blnChange because we now have a new
document with no changes.
Line 13: Reset the caption of the form frmEdit to its initial
state --> Document - Text Editor.
Line 14: Clear the file name of the common dialog control dlgEdit.
This page is getting very long, so you may proceed to Page 21 when you feel ready to complete this application.