End a dialog gracefully in the event of error

I have a CDialog that requires that certain files be opened on creation.  What is the appropriate way to end the dialog if the files are corrupt or missing.

In the instantiation

if(!(inf=fopen...)){
   AfxMessageBox("ERROR...");
   ??????What goes here---I've tried CDialog:OnCancel() and get a missing hWnd error.
}

BTW, I know that I can fire off some messages, and am willing to do just that, but I suspect that I am missing something obvious here.

Thanks in advance, Rick
rickatseasoftAsked:
Who is Participating?
 
AndyAinscowConnect With a Mentor Freelance programmer / ConsultantCommented:
OK
In the OnInitDialog

if(!CheckFiles())
  PostMessage(WM_CLOSE, 0, 0L);

This will allow the dialog to be created and displayed - then it should promptly close itself down via the postmessage
0
 
jkrCommented:
If that is in 'OnInitDialog()' - just return FALSE from your overridden function, i.e.

BOOL MyyCDialog::OnInitDialog ()
{
 if(!(inf=fopen...)){
   AfxMessageBox("ERROR...");
   return FALSE;
 }

  // ...

  return TRUE;
}
0
 
rickatseasoftAuthor Commented:
jkr:

I am under the impression that returning FALSE from OnInitDialog() simply allows me to set the Focus().

Anyway, since you suggested it,  I tried a return of FALSE, and it had no impact at all if it was after the CDialog::OnInitDialog() (it would probably effect focus if I was messing with that).  If I returned FALSE before the call, then it blows up on exit.  In both cases, it still presents the Dialog.  Of course, I can suggest to the user to press Cancel, and disable everything else, but I am looking for a smoother solution.+

What is it that I don't understand?

Rick
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
jkrCommented:
Have you tried calling 'EndDialog()'?
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
Why not test before you open the dialog?
pseudo code

if(CheckFiles())
  {
    CMyDlg dlg(this);
    dlg.DoModal();
....
0
 
rickatseasoftAuthor Commented:
Andy:

I'm trying to make this as self-contained as possible.  Having to check for valid files prior to entry seems like a step that I would like to avoid.  Also, even if I check before hand, there always exists the possibility that the something about the file will have changed in the interim, and it will still fail.

Thanks, Rick
0
 
rickatseasoftAuthor Commented:
jkr:

I've tried CDialog::OnClose(), CDialog::OnCancel(), EndModalLoop().

Rick
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
oh - and do it after you call CDialog::OnInitDialog, not before.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.