Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1347
  • Last Modified:

Should I override "DoModal" to force my dialog to quit if the Init fails?

I have this problem...  My dialog (CDialog) checks a few system variables and settings before displaying anything... So in my InitDialog, I do all the checks and then set a flag called "m_InitSuccess" to TRUE...

Then I want to override DoModal to check the flag and then return IDABORT when that flag is FALSE....

Something like:

int CMyDialog::DoModal()
{
   if (m_InitSuccess)
      return (CDialog::DoModal());
   return (IDABORT);
}

I have two questions about this, first, is this the only (or best) way to cancel out of a dialog? and second, should I just move all my checking code into my DoModal?

Also, what do you think about calling EndDialog(IDABORT) from within the OnInitDialog handler?

-- Bubba
0
bganoush
Asked:
bganoush
  • 7
  • 5
  • 2
  • +1
1 Solution
 
nonubikCommented:
You cannot override DoModal. CDialog would not call yours because it is not a virtual method.

But you can override OnInitDialog()

BOOL CMyDialog::OnInitDialog()
{
...
   if (!m_InitSuccess)
       EndDialog(IDCANCEL);

return TRUE;
}
0
 
bganoushAuthor Commented:

>> You cannot override DoModal. CDialog would not call yours because it is not a virtual method.

That's weird... I just did!

0
 
nonubikCommented:
Well, you didn't
You just declared a method wiht the same name and prototype. You can just call it MyDoModal. This does not mean you override the CDialog::DoModal method
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
bganoushAuthor Commented:

I just double-checked just in case... and yes, I did and it works fine...  I went to the Class Wizard and selected "DoModal", then Add Function and then Edit Function...

The OnInitDialog is what I was thinking about doing... I just wasn't sure if I could safely call EndDialog from there...

-- Bubba
0
 
nonubikCommented:
Try a little test:

CDialog *pDlg = new CMyDialog;
pDlg->DoModal();

//now see what DoModal is called
0
 
nonubikCommented:
Mea culpa, you're right. I just now saw it's virtual
So do not mind my comments.
0
 
bganoushAuthor Commented:

That's what my code looks like and it is called... and breakpoints happen in my DoModal...

I don't think methods have to be virtual to be overridable...

-- Bubba
0
 
bganoushAuthor Commented:

Ok, maybe you're right about the virtual thing!   ;)
0
 
nonubikCommented:
And the answer to your question is yes, you can override. And seems a good way to cancel a dialog.
0
 
bganoushAuthor Commented:

Btw, I always thought that "virtual" applied to a method (as opposed to a class) just meant that the methods MUST be overridden to instantiate the subclass...

I tried this:

#include "stdafx.h"

class class1
{
public:
   class1() {};
  void myFunction();
};

class class2 : public class1
{
public:
   class2() {};
   void myFunction();
};

void class1::myFunction()
{
  printf ("class1\n");
}

void class2::myFunction()
{
   printf ("class2\n");
   class1::myFunction();
}

int main(int argc, char* argv[])
{
  printf("Hello World!\n");
  class2 *yo = new class2;
  yo->myFunction();
  return 0;
}

And it gave me the output:

Hello World!
class2
class1




0
 
nonubikCommented:
"virtual" means that the methods CAN be overriden.
But, in the case of a pure virtual function, it MUST be overriden in the derrived classes. A pure virtual function is declared as

virtual <ret ype> <function name> (<function args>) = 0;

virtual void myFunction() = 0;
0
 
nonubikCommented:
>it MUST be overriden in the derrived classes
to be read as
>it MUST be overriden in the derrived classes in order to instantiate them.
0
 
ZoppoCommented:
Hi,

beside this you can even terminate the dialog in OnInitDialog directly by calling EndDialog, i.e.:

BOOL CMyDlg::OnInitDialog()
{
 ...
 if ( <not ok> )
  EndDialog( IDCANCEL );

 return TRUE;
}

ZOPPO
0
 
ZoppoCommented:
sorry ... should be
  EndDialog( IDABORT );
instead ...
0
 
AndyAinscowCommented:
An alternative

CMyDialog dlg(...);
..
if(dlg.PerformInit())
  dlg.DoModal();

where you call a function to do the initialisation and only enter modal loop if it is succesful.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now