Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Building a Text Editor, Part II

Part 2 of a four-part series on building a text editor in Visual Basic. 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.


advertisement
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 = False End 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 Me End 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 = True End 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.ReadAll AddToMRUList m_strFilename objStream.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.



   
Eric Smith has been a senior consultant with Andersen Consulting, specializing in client/server development using Oracle and Visual Basic, and was senior content developer (Visual Basic) at inquiry.com before it was purchased by Fawcette Technical Publications. He joined the Information Strategies team in September 1997 and is now an independent consultant. He has written or contributed to "Visual Basic 6 Bible" (IDG Books Worldwide, 1998), "Using Visual Basic 6" (Que), and "Inside VBScript with ActiveX" (New Riders Press, 1997). He is certified in Visual Basic 5.0 and Windows Architecture (1 and 2). Reach him here.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap