Solved

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

Posted on 2004-09-23
15
1,199 Views
Last Modified: 2013-11-20
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
Comment
Question by:bganoush
  • 7
  • 5
  • 2
  • +1
15 Comments
 
LVL 16

Expert Comment

by:nonubik
ID: 12135059
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
 

Author Comment

by:bganoush
ID: 12135080

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

That's weird... I just did!

0
 
LVL 16

Expert Comment

by:nonubik
ID: 12135100
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
 

Author Comment

by:bganoush
ID: 12135102

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
 
LVL 16

Expert Comment

by:nonubik
ID: 12135108
Try a little test:

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

//now see what DoModal is called
0
 
LVL 16

Expert Comment

by:nonubik
ID: 12135127
Mea culpa, you're right. I just now saw it's virtual
So do not mind my comments.
0
 

Author Comment

by:bganoush
ID: 12135128

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:bganoush
ID: 12135134

Ok, maybe you're right about the virtual thing!   ;)
0
 
LVL 16

Accepted Solution

by:
nonubik earned 125 total points
ID: 12135170
And the answer to your question is yes, you can override. And seems a good way to cancel a dialog.
0
 

Author Comment

by:bganoush
ID: 12135294

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
 
LVL 16

Expert Comment

by:nonubik
ID: 12135376
"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
 
LVL 16

Expert Comment

by:nonubik
ID: 12135386
>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
 
LVL 30

Expert Comment

by:Zoppo
ID: 12135629
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
 
LVL 30

Expert Comment

by:Zoppo
ID: 12135633
sorry ... should be
  EndDialog( IDABORT );
instead ...
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 12140474
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now