If we want to save the file with the same name we opened it with, we simply close the file. If the name is different, we must open a file (using a different number) with the new name, write the complete random access file, then close it. Like I said, it’s trickier.
•We use both of these code segments in the final example where we write and read sequential files.
1. We now add the capability to read in and save the contents of the text box in the Note Editor application from last class. Load that application. Add a common dialog box to your form. Name it cdlFiles and set the CancelError property to True.
2. Modify the File menu (use the Menu Editor and the Insert button) in your application, such that Open and Save options are included. The File menu should now read:
File
New
Open
Save
Exit
Properties for these new menu items should be:
Caption Name Shortcut
&Open mnuFileOpen [None]
&Save mnuFileSave [None]
3. The two new menu options need code. Attach this code to the mnuFileOpen_Click event. This uses a modified version of the code segment seen previously. We assign the extension ned to our note editor files.
Private Sub mnuFileOpen_Click()
cdlFiles.Filter = "Files (*.ned)|*.ned"
cdlFiles.DefaultExt = "ned"
cdlFiles.DialogTitle = "Open File"
cdlFiles.Flags = cdlOFNFileMustExist + cdlOFNPathMustExist
On Error GoTo No_Open
cdlFiles.ShowOpen
Open cdlFiles.filename For Input As #1
txtEdit.Text = Input(LOF(1), 1)
Close 1
Exit Sub
No_Open:
Resume ExitLine
ExitLine:
Exit Sub
End Sub
And for the mnuFileSave_Click procedure, use this code. Much of this can be copied from the previous procedure.
Private Sub mnuFileSave_Click()
cdlFiles.Filter = "Files (*.ned)|*.ned"
cdlFiles.DefaultExt = "ned"
cdlFiles.DialogTitle = "Save File"
cdlFiles.Flags = cdlOFNOverwritePrompt + cdlOFNPathMustExist
On Error GoTo No_Save
cdlFiles.ShowSave
Open cdlFiles.filename For Output As #1
Print #1, txtEdit.Text
Close 1
Exit Sub
No_Save:
Resume ExitLine
ExitLine:
Exit Sub
End Sub
Each of these procedures is similar. The dialog box is opened and, if a filename is returned, the file is read/written. If Cancel is pressed, no action is taken. These routines can be used as templates for file operations in other applications.
4. Save your application. Run it and test the Open and Save functions. Note you have to save a file before you can open one. Check for proper operation of the Cancel button in the common dialog box.
5. If you have the time, there is one major improvement that should be made to this application. Notice that, as written, only the text information is saved, not the formatting (bold, italic, underline, size). Whenever a file is opened, the text is displayed based on current settings. It would be nice to save formatting information along with the text. This can be done, but it involves a fair amount of reprogramming. Suggested steps:
A. Add lines to the mnuFileSave_Click routine that write the text box properties FontBold, FontItalic, FontUnderline, and FontSize to a separate sequential file. If your text file is named TxtFile.ned, I would suggest naming the formatting file TxtFile.fmt. Use string functions to put this name together. That is, chop the ned extension off the text file name and tack on the fmt extension. You’ll need the Len() and Left() functions.
B. Add lines to the mnuFileOpen_Click routine that read the text box properties FontBold, FontItalic, FontUnderline, and FontSize from your format sequential file. You’ll need to define some intermediate variables here because Visual Basic won’t allow you to read properties directly from a file. You’ll also need logic to set/reset any check marks in the menu structure to correspond to these input properties.
C. Add lines to the mnuFileNew_Click procedure that, when the user wants a new file, reset the text box properties FontBold, FontItalic, FontUnderline, and FontSize to their default values and set/reset the corresponding menu check marks.
D. Try out the modified application. Make sure every new option works as it should.
Actually, there are ‘custom’ tools (we’ll look at custom tools in Class 10) that do what we are trying to do with this modification, that is save text box contents with formatting information. Such files are called ‘rich text files’ or rtf files. You may have seen these before when transferring files from one word processor to another.
6. Another thing you could try: Modify the message box that appears when you try to Exit. Make it ask if you wish to save your file before exiting - provide Yes, No, Cancel buttons. Program the code corresponding to each possible response. Use calls to existing procedures, if possible.
Saturday, December 26, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment