Solved

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

Posted on 2004-09-23
15
1,277 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 

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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MFC Dialog 9 54
How to convert MFC::CString to UTF8 wchar_t* 10 461
wordmultiple challenge 12 170
Way to decrease size of apk file 9 132
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

759 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