?
Solved

CFileDialog save/replace problem?

Posted on 2005-05-02
5
Medium Priority
?
1,022 Views
Last Modified: 2013-11-20
Hi

I am working with CFileDialog in MFC SDI application. When I open a file for ex: test.txt from File->Open menu, the contents of the file are read and displayed on the view. When I select, File->Save option  to save the contents of the file, a CFileDialog is displayed. When the selected file from CFileDialog is the same file as the opened one i.e test.txt, then a special message should be displayed to the user and the default windows warning message asking for replacing the file should not be displayed. However if any different already existing file is selected, then the default windows warning message should be displayed. In other words, I want to ovveride the function that is called when we click on CFileDialog's save button.

Can anyone please give me some inputs for doing this?

Thanks
0
Comment
Question by:osi-sys
  • 3
  • 2
5 Comments
 
LVL 16

Expert Comment

by:nonubik
ID: 13915639
You can derive your own class from CFileDialog and override CFileDialog::OnFileNameOK(). There you can decide to display the mesasge box or not.
0
 

Author Comment

by:osi-sys
ID: 13915762
Hi nonubik ,

Thanks for your timely response.
Yes, I exactly did the same way as said by you.  

I derived a class from CFileDialog and overrided CFileDialog::OnFileNameOK().

m_LogFileDialog = new CLogFileDialog(FALSE, _T("log"), path, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("Log Files (*.log)|*.log||"));

BOOL CLogFileDialog::OnFileNameOK()
{
   CString path_name = GetPathName();
    if(path_name.GetLength() > 0)
   {
      if(m_LogFileName.CompareNoCase(path_name) == 0)
           AfxMessageBox(_T("The file you selected is the current log file opened, please choose another file"));      
           
      else
         return CFileDialog::OnFileNameOK();
   }
}


When I clicked on save button on CFileDialog, the default window warning message saying, “ File name already exists, Do you want to replace the existing file” is displayed first  
and then my message AfxMessageBox(_T("The file you selected is the current log file opened, please choose another file")) is shown next.

The I removed OFN_OVERWRITEPROMPT style during the creation of FileDialog, then only my message was shown. But what I want is that, when I try to save to the file that is currently opened, then only my message should be shown, and in all other cases when an already existing file is selected (other than the currently opened file), the  default window warning message should be displayed.

Can you please help me furher now ?

Thanks
Madhavi  
0
 
LVL 16

Accepted Solution

by:
nonubik earned 400 total points
ID: 13915918
BOOL CLogFileDialog::OnFileNameOK()
{
   CString path_name = GetPathName();
    if(path_name.GetLength() > 0)
   {
      if(m_LogFileName.CompareNoCase(path_name) == 0)
      {
           AfxMessageBox(_T("The file you selected is the current log file opened, please choose another file"));      
           return 1; //need to return here 1, too
      }      
      else
      {
         //check if the file exist and display the "standard" message box
         if(FileExists(path_name))
            if(AfxMessageBox(_T("Overwrite file... ?"), MB_YESNO) == IDYES)
              return CFileDialog::OnFileNameOK();
            else return 1; //forbid

         return CFileDialog::OnFileNameOK();
      }
   }
}


The FileExist() function needs to be written. You can check for example with GetFileAttributes and if it returns 0xffffffff it is a failure (e.g. file not found).
0
 

Author Comment

by:osi-sys
ID: 13915977
Thanks nonubik, It's working fine now
0
 
LVL 16

Expert Comment

by:nonubik
ID: 13915983
Glad to hear that.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses
Course of the Month15 days, 18 hours left to enroll

850 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