n answering questions for Ask the VB Pro, I've been noticing a lot of the same types of questions. These questions primarily relate to basic operations like opening and writing to files, tying one form to the next, using menus, and so on. To help eliminate some of these questions, this article will present a "model" application that will exercise lots of the features you've been asking about. This part of the article will show you how to use the Common Dialog control as part of a text editor application. The code will be made available for each part so you can follow along.
Future parts of this article series will teach you how to provide find/replace functions, cut/copy/paste, a most recently used list of files for the File menu, and more.
The first thing we have to do is design the form we'll be using. It's a simple looking form, as shown in Figure 1.
In this part, we're going to concentrate on getting the file operations working using the Common Dialog control. To get started, create a new Standard EXE project in Visual Basic. All the references in this article apply to Visual Basic 6.0. If you're using an older version, you'll have to make some minor changes to these instructions (you'll see where). Once the project is created, you need to add the Common Dialog control to the Toolbox by selecting Components from the Project menu. Add "Microsoft Common Dialog Control 6.0" and then click OK.
The form is pretty easy to build: a form and a text box. Here are the steps:
1. Draw a TextBox control starting at the upper left-hand corner of the form. Leave a small margin at the top and left of the box.
2. Name the control txtData in the Properties window.
3. Delete the text in the Text property of txtData.
4. Change the MultiLine property to True.
5. Change the ScrollBars property to Vertical.
The next thing you need is the form resizing code. The code is easy to write and understand:
Private Sub Form_Resize()
On Error Resume Next
txtData.Width = Me.ScaleWidth - (2 * txtData.Left)
txtData.Height = Me.ScaleHeight - (2 * txtData.Top)
We basically want the text box to fill up the form as we resize the form. This code, which has been shown before in this column, resizes the box to have a small margin around the edge. The ScaleWidth and ScaleHeight properties of the form give us the available space we have, and we subtract a little bit of it to provide the margin on top and bottom (which is why we multiply the left and top by 2).
Next, we need to add our menu choices. We'll concentrate on the File operations for this article. Here are the entries you should add:
Caption Name Shortcut Key
&File mnuFile None
&New mnuFileNew Ctrl+N
&Open mnuFileOpen Ctrl+O
&Save mnuFileSave Ctrl+S
Save &As mnuFileSaveAs None
- mnuFileSep None
E&xit mnuFileExit None
Note: Indent the choices listed under the File menu so that they show up as part of the File menu. Use the small right arrow button on the Menu Editor dialog to make this work.
In order to make some of the features work properly, we need a couple of form-level variables, which should be declared in the General Declarations section of the form:
Private m_blnDirty As Boolean
Private m_strFilename As String
The first variable is used to mark whether the form's data has been changed or not. The second is used to store the filename for the text being created.
We'll next write the Save routine, which will need to be called from several locations. At any time, if the user has changed data and wants to exit, look at another file, or create another file, we need to prompt him/her to save the current data first. To make this work, we have to figure out when data changes. The m_blnDirty variable (the m prefix means module, since the variable is declared for the whole form) is a flag that will be set whenever changes are made to the text. By setting the m_blnDirty flag to True in the Change event of the txtData control, we know when data has changed.
There will be several places in which we have to ask the user whether they want to save the data. The user has a few options:
Yes - Save data and continue doing whatever the user wanted to do, whether that was to create a new document, open another, or exit.
No - Don't save data but continue doing the user's request.
Cancel - Go back to where the user was before making the request.
The SaveComplete function in the code determines whether the calling code should continue or not. The only time the calling code can't continue doing what it wanted to is if the user pressed the Cancel button.
The next step, once we've determined that the user wants to save the data, is to save the data to a text file. We're going to use the FileSystemObject to do this, but before you can use it, you have to reference it in your project. Follow these steps:
1. Select References from the Project menu.
2. Scroll down until you see Microsoft Scripting Runtime in the list.
3. Check the box next to it and then click the OK button.
The SaveFile routine in the form is used to prompt the user for a filename if one isn't in the m_strFilename variable. It then sets a number of properties on the Common Dialog control and shows the dialog. Once the filename is retrieved, we open the text file as a writeable file, write the data to it, and close the file. The FileSystemObject (objFSO in the code) allows us to create a TextStream object (objStream), which lets us write our data to the file the user picked.
The last part of the SaveFile routine is a basic error handler. The Common Dialog control's CancelError flag was set, which means an error should be generated if the user hits the Cancel button. We have to trap that error as well as any others that might occur.
The last code we'll cover in this article is to hook the subroutine up to the appropriate menu choices. Here's the code you need to add:
Private Sub mnuFileSave_Click()
Private Sub mnuFileSaveAs_Click()
Dim strTempFilename As String
' In case the user cancels out
' of the Save dialog, keep the
' old filename for use. Otherwise
' the new name will be used.
strTempFilename = m_strFilename
m_strFilename = ""
If m_strFilename = "" Then m_strFilename = strTempFilename
If the user hits the Save button, we either save to the existing filename or prompt the user for a filename. If the user wants to "Save As", we have to preserve the old filename until we know the user has selected a new filename. Other than that, the save routine is identical.
In the next article, we'll cover how to open a file and how to create a new file in our text editor. We'll also cover how to create a "most recently used" file list at the end of the File menu. This handy feature is common to most applications and saves the user time when they want to reopen a recently used file.
Download the code from this Solution here.