Building a Text Editor, Part II

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:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing