• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

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
0
rickatseasoft
Asked:
rickatseasoft
  • 3
  • 3
  • 2
1 Solution
 
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
 
jkrCommented:
Have you tried calling 'EndDialog()'?
0
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.

 
AndyAinscowCommented:
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
 
AndyAinscowCommented:
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
 
AndyAinscowCommented:
oh - and do it after you call CDialog::OnInitDialog, not before.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now