Link to home
Start Free TrialLog in
Avatar of johndill
johndill

asked on

dialog boxes and file i/o

File I/0

I’m a little confused about file saving and save as;  The below program opens up text files with out a problem.  I’ve managed to cal the save (and saveAs) dialog boxes.  I’d like to have the save function to overwrite the existing file using the previous file name, providing the file has not been saved before (how can I tell this?) .  How would I add a msgbox that would ask the user if he would like to save any changes befor exiting?



Private Sub Command1_Click()

End Sub

Private Sub cmdCloseFile_Click()
   
        Close
        cmdOpenFile.Enabled = True
        txtWorkPad.Text = ""
        txtFileNameOpen.Text = ""
End Sub

Private Sub cmdOpenFile_Click()

    On Error GoTo DialogError
   
    cmOpenFile.ShowOpen
    Open cmOpenFile.filename For Input As 1
    txtWorkPad.Text = Input(LOF(1), 1)
    txtFileNameOpen = cmOpenFile.filename
   
    cmdOpenFile.Enabled = False
   
DialogError:
    On Error GoTo 0
    Exit Sub
   
End Sub


Private Sub Form_Load()

' Set up the common dialog control to open a text file

    On Error GoTo DialogError

    With cmOpenFile
        .CancelError = True
        .Filter = "Text Files (*.txt)|*.txt"
        .FilterIndex = 1
        .DialogTitle = "Select a Text File"
    End With

DialogError:
    On Error GoTo 0
    Exit Sub

End Sub

Private Sub mnuEditReplace_Click()

   frmSearchString.Show 1

   
End Sub

Private Sub mnuFileClose_Click()

' Close all open files
        Close
' reset the menus and the caption bar

        mnuFileOpen.Enabled = True
        txtWorkPad.Text = ""
        frmWordPad.Caption = "WordPad"
        mnuFileClose.Enabled = False
        mnuFileSave.Enabled = False


End Sub


Private Sub mnuFileExit_Click()
    End
End Sub


Private Sub mnuFileOpen_Click()

    On Error GoTo DialogError

' Common dialog has been set up in form load event
        cmOpenFile.ShowOpen
' take filename selected by user and open it up
        Open cmOpenFile.filename For Input As 1
' read the whole file into the text box
        txtWorkPad.Text = Input(LOF(1), 1)
       
        frmWordPad.Caption = "WordPad " + cmOpenFile.filename
' don't allow another file to be opened
        mnuFileOpen.Enabled = False
' however we can save or close the file
        mnuFileClose.Enabled = True
        mnuFileSave.Enabled = True
        mnuFileSaveAs.Enabled = True
       
DialogError:
        On Error GoTo 0
        Exit Sub
   
    End Sub






Private Sub mnuFileSave_Click()

    On Error GoTo DialogError

'Open up a save file dialog using the previous properties, except title
        cmOpenFile.DialogTitle = "Save Your Text File"
        cmOpenFile.ShowSave
       
        Open cmOpenFile.filename For Output As 1
' put the whole text box into the file
        Print #1, txtWorkPad.Text
' now update the filename on the caption
        frmWordPad.Caption = "WordPad " + cmOpenFile.filename
        mnuFileClose.Enabled = True
        Open cmOpenFile.filename For Output As 1
       
DialogError:
    On Error GoTo 0
    Exit Sub
    Close
   
End Sub

Private Sub mnuFileSaveAs_Click()
On Error GoTo DialogError

'Open up a save file dialog using the previous properties, except title
        cmOpenFile.DialogTitle = "Save As"
        cmOpenFile.ShowSave
       
        Open cmOpenFile.filename For Output As 1
' put the whole text box into the file
        Print #1, txtWorkPad.Text
' now update the filename on the caption
        frmWordPad.Caption = "WordPad " + cmOpenFile.filename
        mnuFileClose.Enabled = True
       
DialogError:
    On Error GoTo 0
    Exit Sub
    Close

End Sub
Avatar of Sargon
Sargon

All this for 50 points?
Avatar of johndill

ASKER

Well as you can see I wrote most of the code myself.  All I need help with is the mechanics of Save ans Save As.  I've already crested every thing else.  What say you to 70?
At no point in your code example, have you used FLAGS.

I've had a brief view of the code, it's a little to much for one question and 70 points.

Look in the Help File for the FLAGS property of the Common Dialog Control. This will assist in with most the code.

PS.......If you are opening and Closing a text file, then why not use the RichTextFormat control. It's easier, faster and more code efficiant. Note that it 32bit only.



I'd need 150 points for this one.
Thanks I'll try the flags.  I can only offer 90 at this point as my account is tapped
ASKER CERTIFIED SOLUTION
Avatar of weekee
weekee

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial