Link to home
Start Free TrialLog in
Avatar of osi-sys
osi-sys

asked on

CFileDialog save/replace problem?

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
Avatar of nonubik
nonubik

You can derive your own class from CFileDialog and override CFileDialog::OnFileNameOK(). There you can decide to display the mesasge box or not.
Avatar of osi-sys

ASKER

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  
ASKER CERTIFIED SOLUTION
Avatar of nonubik
nonubik

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
Avatar of osi-sys

ASKER

Thanks nonubik, It's working fine now
Glad to hear that.