Building a Text Editor, Part III

Building a Text Editor, Part III

n this article, you’ll continue building on the text editor that you created in Part I and Part II. As usual, the code is available for download by clicking here. This article will add a few more features to the application, including the ability to pick a file from the most-recently-used list (MRU) and automatically open it. To save space, the complete code listing won’t be shown in this article, so be sure to download it before reading the article.

Right now, the code to open a file is located in the event handler for the File->Open menu choice. If we want to allow the user to pick a file from the list and open it, we need to make the code a bit more modular. In addition, when the user picks a file, we don’t have to show the Common Dialog to allow the user to pick a file. In this case, we’re going to take the code that actually loads the file and put it in a subroutine that can be called from multiple places. The filename that the user picks will still be stored in the module level variable m_strFilename, so that means we don’t have to pass that variable as a parameter to our new LoadFile subroutine that you’ll see shortly. The menu choice for File->Open will still show the common dialog, but the menu choice for the MRU list won’t need to.

Here is the new mnuFileOpen_Click event handler, with the subroutine call bolded:

Private Sub mnuFileOpen_Click()      If Not SaveComplete() Then Exit Sub      '   ' Select file to open using   ' Common Dialog control   '   On Error GoTo EH   With ComDlg      .CancelError = True      .Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"      .DefaultExt = ".txt"      .DialogTitle = "Open File"      .FilterIndex = 1      .Flags = cdlOFNHideReadOnly      .ShowOpen      If .FileName <> "" Then m_strFilename = .FileName   End With      LoadFile   AddToMRUList m_strFilename   Exit Sub      '   ' This will catch any errors, such   ' as when the user hits the Cancel   ' button to exit the dialog.   'EH:   If Err.Number = cdlCancel Then      Exit Sub   Else      MsgBox "ERROR: " & Err.Description _         & ". [Error #" & Err.Number & "]", vbCritical   End If   End Sub

The last change we need to put in is to create an event handler for when a user clicks a file on the MRU list. With the LoadFile subroutine in place, this code is easy to write. The key is that the filename to open is already available to uswe just have to read it from the Caption property of the menu choice that is selected.

Here’s the code you need to add:

Private Sub mnuFileMRU_Click(Index As Integer)   If Not SaveComplete() Then Exit Sub      m_strFilename = Mid(mnuFileMRU(Index).Caption, 4)   LoadFileEnd SubIf you look at the menu, the format of an entry in the MRU list is as follows:#[space]filename

However, in order to get the number to be underlined and be selectable, we have an ampersand preceding the number in the Caption property. For this reason, the filename starts at the fourth, not third, character of the Caption property. The Mid function, using only the two arguments, will start at character 4 and retrieve everything to the end. We then call the LoadFile routine, as we did before. Before we do that, we check to make sure that any changes have been saved. Once you have two files in your MRU list, you can switch back and forth like any other application.

In the next part of this article, you’ll learn to create a find function that will both find text and allow you to find other occurrences of that same piece of text.

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