Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FILE menu system (Open, Save, Save As etc.) for opening/writing textbox to a text file

Folders and Files

FILE menu system (Open, Save, Save As etc.) for opening/writing textbox to a text file

by  JESTAR  Posted    (Edited  )
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:
[tt]
[color green] caption name index[/color]
&File mnuFileTop
&New mnuFile 0
&Open mnuFile 1
&Save mnuFile 2
Save &As mnuFile 3
- mnuFile 4
&Exit mnuFile 5
[/tt]

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 ]--------------------
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top