Solved

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

Posted on 2004-09-23
15
1,218 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need to retreive data from .TFS file exported by DVR 8 177
Unix Command -- Challenging  question 7 86
Problem to open Excel file 15 95
Message not shown 5 29
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 …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

914 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

22 Experts available now in Live!

Get 1:1 Help Now