Visual Basic Programming

Page 20

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...

The grayed menu items are disabled. Simply uncheck the Enabled checkbox to disable them.

  • Open the code window for the form object.
  • Select the Resize event from the event combo box.
  • Press tab, then type the following lines:

    txtEdit.Height = frmEdit.ScaleHeight
    txtEdit.Width = frmEdit.ScaleWidth
    txtEditWrap.Height = frmEdit.ScaleHeight
    txtEditWrap.Width = frmEdit.ScaleWidth

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.

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.

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.

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.

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.

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.

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.

You know what this means by now, so no explaining is necessary--I hope!

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.

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.

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.

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.

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.

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.

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.

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

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.

Top of Page

Page 1 | Page 2 | Page 3 | Page 4 | Page 5 | Page 6 | Page 7 | Page 8 | Page 9 | Page 10 | Page 11

Page 12 | Page 13 | Page 14 | Page 15 | Page 16 | Page 17 | Page 18 | Page 19 | Page 20

Page 21 | Page 22 | Page 23 | Page 24 | Page 25 | Page 26 | Page 27 | Page 28 | Page 29 | Page 30