Solved

dialog boxes and file i/o

Posted on 1997-03-18
7
264 Views
Last Modified: 2008-03-17
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
0
Comment
Question by:johndill
7 Comments
 

Expert Comment

by:Sargon
Comment Utility
All this for 50 points?
0
 

Author Comment

by:johndill
Comment Utility
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?
0
 

Expert Comment

by:limacher
Comment Utility
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.


0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:johndill
Comment Utility

0
 
LVL 2

Expert Comment

by:dirkmartin
Comment Utility
I'd need 150 points for this one.
0
 

Author Comment

by:johndill
Comment Utility
Thanks I'll try the flags.  I can only offer 90 at this point as my account is tapped
0
 
LVL 2

Accepted Solution

by:
weekee earned 90 total points
Comment Utility
Basically you are trying to detect whether a user has save a file or. My approach is not very classic but it si simple. Simply creat a variable called saveFlag as boolean. Every time i open a new file  and the text is modified i set the flag to false. This is done by setting the flag to false in the keypress event of the textbox where the text is displayed. So what you need to do is compared whether this flag is False before exiting. If it is false, prompt the user. Once the file is save, set the flag to true...
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now