Building a Text Editor, Part II

n Part I of this article, we built our basic form for our text editor and wrote the Save functionality. In this part, we complete the File menu with the New and Open choices, as well as a most-recently-used file list to provide quick access to those files. The code is available for download so you don’t have to retype it all. In addition, not all of the code in the application is shown in the article, due to space restrictions.

Creating a new file is simply a matter of erasing the box after first prompting the user to save any existing data. Here’s the code for the New menu choice:

Private Sub mnuFileNew_Click()   If Not SaveComplete() Then Exit Sub      '   ' Clear out the text box   ' and reset all variables   '   m_strFilename = ""   txtData.Text = ""   m_blnDirty = FalseEnd Sub

Note that we change the dirty flag to False after we change the text box. If we did it the other way around, the dirty flag would be set to True when the text box’s contents changed.

Opening a file is similar to saving: Get a filename, open the file, and show it. Since we only open files from one place, the code is in the File->Open menu handler. We first prompt the user to save data if any changes have been made. We then configure the Common Dialog control to show an Open File dialog, using some basic options that have been covered in other articles. Finally, we use the FileSystemObject (objFSO) to open up the selected text file, and using the ReadAll method, we read all the text into the text box in one easy step. Like the Save routine, there is an error handler at the end of the code to catch any errors or to trap when the user hits the Cancel button on the Common Dialog control.

We also have to add a little bit of code for the Exit choice. That code is shown here:

Private Sub mnuFileExit_Click()   Unload MeEnd Sub

Another feature we need to support is common to most applications. If you’re working in Microsoft Word and hit Exit or the close box, Word prompts you to save your documents before exiting. This functionality is easy to add using the QueryUnload event. This event is triggered whenever the form is instructed to unload, whether that is from the Unload Me statement, Windows shutting down, or a click in the close box. The code you need here is as follows:

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)   If Not SaveComplete Then Cancel = TrueEnd Sub

We use the SaveComplete routine created in the first part of the article to determine if it’s safe to proceed. If the user hits Cancel on the save prompt, we set the QueryUnload event’s Cancel parameter to True, which stops whatever Exit prompted the event in the first place. If you’re exiting Windows the normal way, setting Cancel to True here will stop Windows from exiting. It’s a very handy thing to have in your application.

At this point, you have all the code to create, open, and save files. Give the application a try and see what you can do with it. It’s quite fast and easy to add to, as you’re going to do shortly.

In applications such as Microsoft Word, there is a feature on the File menu called a Most Recently Used file list. This list shows the last four (typically) files you opened in the application. We’re going to create a similar feature for our application.

To make it work, we have to first keep track of how many files are in the list. For this we’ll use another module level variable:

Private m_intMRU As Integer

We then have to configure our File menu with some more choices. In a typical File menu, there is a separator bar between the Exit choice at the bottom and other choices at the top. If you have a MRU list, there is another separator before those items. In your File menu, add a choice called mnuFileMRU with an index of zero. The Caption for this menu item should be a single dash, which will create a separator bar. Since we don’t want to show the separator if there are no files in the list, mark this choice as invisible at startup.

The AddToMRUList subroutine is responsible for reconfiguring the MRU list as new files are opened. You pass in a filename to it, and it first figures out how many items there are in the list. If there are less than four, it uses the Load statement to create a new menu item. We then have to shuffle all the names down; that is, #1 becomes #2, #2 becomes #3, #3 becomes #4, and #4 is dropped if we already have four. Each menu item will look like this:

&1 Filenamegoeshere.txt

The ampersand causes the 1 to be underlined in the menu choice. When we shuffle the choices down, we have to remove the ampersand and number before putting the new number on. Once the old choices are shuffled down, we store the new one in spot #1.

This subroutine is called whenever you open a file in the text editor, so make a minor change to your mnuFileOpen_Click subroutine, where marked here:

txtData.Text = objStream.ReadAllAddToMRUList m_strFilenameobjStream.Close

At this point, each time you open a file, it will be added to the list. In the next part of the article, you’ll see how to handle the case in which the user picks a file from that list to reopen.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may