Solved

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

Posted on 2004-09-23
15
1,235 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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
 

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 31

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 31

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Strange date stored 22 118
Add content to output file 4 63
Prevent this page from creating additional dialogs. 3 512
either24  challenge 19 96
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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

813 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

17 Experts available now in Live!

Get 1:1 Help Now