Freetutes.com

VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

VB .NET - Learn Visual Basic .NET

Systems Analysis - System analysis and Design tutorial for Software Engineering

Browse Topics

- Getting started
- Data Types
- Modules
- Operators in VB6
- VB6 Variable
- VB6 Procedures
- VB6 Control Structures
- Loops in VB6
- VB6 Exit Do & With End With
- Arrays in VB6
- User-Defined Data Types
- VB6 Constants
-
VB6 Built-in Functions
- Date and Time in VB6
- VB6 Controls
- TextBox Control
- ComboBox & OptionButton
- Label & Frame
- PictureBox & ImageBox
- Timer Control
- ListBox & ComboBox
- VB6 ScrollBar
- Control Arrays in VB6
- Files controls in VB6
- VB6 CheckBox
- Forms in VB6
- Menus in VB6
- MDI Form in VB6
- InputBox
- MessageBox
- Mouse events
- Mouse Move
- Error Handling
-
Error Handling (2)
-
VB6 Database

You are here: Visual Basic > VB6 (Beginners Tutorial)

Tutorial Main Page | Previous Page | Contents | Next Page

Example - Note Editor - Reading and Saving Text 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.


  

Tutorial Main Page | Previous Page | Contents | Next Page

   

Home | Link to Us | About Us | Privacy Policy | Contact Us

Copyright © Freetutes.com | All Rights Reserved