×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Visual Basic (Classic) FAQ

Folders and Files

FILE menu system (Open, Save, Save As etc.) for opening/writing textbox to a text file by JESTAR
Posted: 21 Jul 03 (Edited 14 Apr 04)

What is this FAQ?
This FAQ is to create a FILE menu for your application to write a textbox to a textfile, just like ones in all applications. The menu includes:
New - Checks if a file is already being edited before creating a new file
Open - Checks if a file is open before allowing the user to open a new file
Save - Automatically checks if the file has been previously saved, displaying the ShowSave dialog if necessary
Save As - Allows the user to save the file as something different
Exit - Exits your application, checking if the user wishes to save the file before closing

The example I'm using here is with a menu system as a control array, as follows:

caption             name             index
 &File             mnuFileTop         
    &New           mnuFile                  0
    &Open          mnuFile                  1
    &Save          mnuFile                  2
    Save &As       mnuFile                  3
    -              mnuFile                  4
    &Exit          mnuFile                  5


Our textbox is named TextBox. We must assure that the user can only open *.txt files, Word files will appear as symbols. Make the textbox about 3/4 the width of the screen, and 3/4 the height of the screen, or whatever dimensions you want. Now set the Multiline property to True, and the Scrollbars property to Vertical.

Also drop a CommonDialog control onto the form, naming it CommonDialog.

CODE

Private Sub mnuFile_Click(Index As Integer)
    Dim strName As String, strFile As String, strTemp As String
    Dim Answer As Integer

    Select Case Index
        Case 0:     ' NEW
            ' If a file is already open, offer the user the option to save it before creating a new file
            On Error GoTo ErrorHandler
            ' Check if there is a file already open
            If TextBox.Text <> "" Then
                Answer = MsgBox("Changes made to the file you are currently working on will be lost. Do you wish to save this file before creating another?", vbYesNoCancel, "Save your file?")
                Select Case Answer
                    Case vbYes:
                        ' Save the existing file if the user selects YES then clear the textbox
                        If CommonDialog.FileName = "" Then        ' The file hasn't been saved before
                            With CommonDialog
                                .Filter = "Text (.txt)|*.txt"
                                .InitDir = "C:\My Documents"
                                .DialogTitle = "Save your file..."
                                .ShowSave
                            End With
                            strName = CommonDialog.FileName
                            Open strName For Output As #1
                            Print #1, TextBox.Text
                            Close #1
                        Else                        ' The file has been saved before
                            strName = CommonDialog.FileName
                            Open strName For Output As #1
                            Print #1, TextBox.Text
                            Close #1
                            MsgBox "Your file has been saved as " & CommonDialog.FileTitle, vbOKOnly, "File save confirmation"
                        End If
                        TextBox.Text = ""
                        TextBox.SetFocus
                    Case vbNo:
                    ' Don't save existing file if user selects NO and clear the textbox
                        Close #1
                        TextBox.Text = ""
                    Case vbCancel:
                        TextBox.SetFocus
                End Select
            End If
                       
        Case 1:     ' OPEN
            On Error GoTo ErrorHandler
            ' Check if there is a file already open
            If TextBox.Text <> "" Then
                Answer = MsgBox("Changes made to the file you are currently working on will be lost. Do you wish to save this file before opening another?", vbYesNoCancel, "Save your file?")
                    Select Case Answer
                        Case vbYes:
                        ' Save the existing file if the user selects YES before opening a new file
                            If CommonDialog.FileName = "" Then        ' The file hasn't been saved before
                            With CommonDialog
                                .Filter = "Text (.txt)|*.txt"
                                .InitDir = "C:\My Documents"
                                .DialogTitle = "Save your file..."
                                .ShowSave
                            End With
                            strName = CommonDialog.FileName
                            Open strName For Output As #1
                            Print #1, TextBox.Text
                            Close #1
                        Else                        ' The file has been saved before
                            strName = CommonDialog.FileName
                            Open strName For Output As #1
                            Print #1, TextBox.Text
                            Close #1
                            MsgBox "Your file has been saved as " & CommonDialog.FileTitle, vbOKOnly, "File save confirmation"
                        End If
                        ' File is now saved, we can open the new file
                            With CommonDialog
                                .Filter = "Text (.txt)|*.txt"
                                .InitDir = "C:\My Documents"
                                .DialogTitle = "Open a file..."
                                .ShowOpen
                            End With
                            strName = CommonDialog.FileName
                            Open strName For Input As #1
                            strFile = ""
                            Do Until EOF(1)
                                Line Input #1, strTemp
                                strFile = strFile & strTemp & vbCrLf
                            Loop
                            TextBox.Text = strFile
                            Close #1
                        ' The new file is now opened into the textbox
                        Case vbNo:
                        ' Don't save existing file if user selects NO and open an new file
                            With CommonDialog
                                .Filter = "Text (.txt)|*.txt"
                                .InitDir = "C:\My Documents"
                                .DialogTitle = "Open a file..."
                                .ShowOpen
                            End With
                            strName = CommonDialog.FileName
                            Open strName For Input As #1
                            strFile = ""
                            Do Until EOF(1)
                                Line Input #1, strTemp
                                strFile = strFile & strTemp & vbCrLf
                            Loop
                            TextBox.Text = strFile
                            Close #1
                        Case vbCancel:
                            TextBox.SetFocus
                    End Select
            Else
                ' There is no file open to begin with, so open a new file
                With CommonDialog
                    .Filter = "Text (.txt)|*.txt"
                    .InitDir = "C:\My Documents"
                    .DialogTitle = "Open a file..."
                    .ShowOpen
                End With
                strName = CommonDialog.FileName
                Open strName For Input As #1
                strFile = ""
                Do Until EOF(1)
                    Line Input #1, strTemp
                    strFile = strFile & strTemp & vbCrLf
                Loop
                TextBox.Text = strFile
                Close #1
            End If
            
        Case 2:     ' SAVE
        On Error GoTo ErrorHandler
        If CommonDialog.FileName = "" Then        ' The file hasn't been saved before
            With CommonDialog
                .Filter = "Text (.txt)|*.txt"
                .InitDir = "C:\My Documents"
                .DialogTitle = "Save your file..."
                .ShowSave
            End With
            strName = CommonDialog.FileName
            Open strName For Output As #1
            Print #1, TextBox.Text
            Close #1
        Else                        ' The file has been saved before
            strName = CommonDialog.FileName
            Open strName For Output As #1
            Print #1, TextBox.Text
            Close #1
            MsgBox "Your file has been saved as " & CommonDialog.FileTitle, vbOKOnly, "File save confirmation"
        End If
        
        Case 3:     ' SAVE AS
            On Error GoTo ErrorHandler
            With CommonDialog
                .Filter = "Text (.txt)|*.txt"
                .InitDir = "C:\My Documents"
                .DialogTitle = "Save your file..."
                .ShowSave
            End With
            strName = CommonDialog.FileName
            Open strName For Output As #1
            Print #1, TextBox.Text
            Close #1
            
        Case 5:     ' EXIT
        ' If a file is already open, offer the user the option to save it before exiting
            On Error GoTo ErrorHandler
            ' Check if there is a file already open
            If TextBox.Text <> "" Then
                Answer = MsgBox("Changes made to the file you are currently working on will be lost. Do you wish to save this file before creating another?", vbYesNoCancel, "Save your file?")
                Select Case Answer
                    Case vbYes:
                        ' Save the existing file if the user selects YES then close the application
                        If CommonDialog.FileName = "" Then        ' The file hasn't been saved before
                            With CommonDialog
                                .Filter = "Text (.txt)|*.txt"
                                .InitDir = "C:\My Documents"
                                .DialogTitle = "Save your file..."
                                .ShowSave
                            End With
                            strName = CommonDialog.FileName
                            Open strName For Output As #1
                            Print #1, TextBox.Text
                            Close #1
                            ' File saved, close the app
                            Unload Me
                            End
                        Else                        ' The file has been saved before
                            strName = CommonDialog.FileName
                            Open strName For Output As #1
                            Print #1, TextBox.Text
                            Close #1
                            MsgBox "Your file has been saved as " & CommonDialog.FileTitle, vbOKOnly, "File save confirmation"
                        End If
                        ' File saved, close the app
                        Unload Me
                        End
                    Case vbNo:
                    ' Don't save existing file if user selects NO and close the app
                        Close #1
                        Unload Me
                        End
                    Case vbCancel:
                        TextBox.SetFocus
                End Select
            End If
        
            Unload Me
            End
        End Select
        
        Exit Sub
ErrorHandler:
    
End Sub



Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Dim strName As String, strFile As String, strTemp As String
    Dim Answer As Integer
' If a file is already open, offer the user the option to save it before exiting
            On Error GoTo ErrorHandler
            ' Check if there is a file already open
            If TextBox.Text <> "" Then
                Answer = MsgBox("Changes made to the file you are currently working on will be lost. Do you wish to save this file before creating another?", vbYesNoCancel, "Save your file?")
                Select Case Answer
                    Case vbYes:
                        ' Save the existing file if the user selects YES then close the application
                        If CommonDialog.FileName = "" Then        ' The file hasn't been saved before
                            With CommonDialog
                                .Filter = "Text (.txt)|*.txt"
                                .InitDir = "C:\My Documents"
                                .DialogTitle = "Save your file..."
                                .ShowSave
                            End With
                            strName = CommonDialog.FileName
                            Open strName For Output As #1
                            Print #1, TextBox.Text
                            Close #1
                            ' File saved, close the app
                            Unload Me
                            End
                        Else                        ' The file has been saved before
                            strName = CommonDialog.FileName
                            Open strName For Output As #1
                            Print #1, TextBox.Text
                            Close #1
                            MsgBox "Your file has been saved as " & CommonDialog.FileTitle, vbOKOnly, "File save confirmation"
                        End If
                        ' File saved, close the app
                        Unload Me
                        End
                    Case vbNo:
                    ' Don't save existing file if user selects NO and close the app
                        Close #1
                        Unload Me
                        End
                    Case vbCancel:
                        TextBox.SetFocus
                End Select
            End If
        
            Unload Me
            End
        
        
        Exit Sub
ErrorHandler:

End Sub
 



------------------------[ END ]--------------------

Back to Visual Basic (Classic) FAQ Index
Back to Visual Basic (Classic) Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close