Implementing OnOK() in base CDialog class

Hi,

If I have a few CDialog derived classes deriving from a base CDialog class, is there any way to have them all block the OnOK() function from 'closing' the dialog? They are all child dialogs and I don't want the user to be able to hit enter and accidentally close the dialog. And I don't want to have to write the same code in every derived class.

Thanks
DJ_AM_JuiceboxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
class CMyBaseDlg : public CDialog
...


class CMyDlg1 : public CMyBaseDlg
...


class CMyDlg2 : public CMyBaseDlg
...


and so on.
In your CMyBaseDlg have the common OnOK handler which does NOT call CDialog::OnOK()
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Deepu AbrahamR & D Engineering ManagerCommented:
OnOK is typically overridden for modeless but not modal dialog boxes. If you override this handler for a modal dialog box, call the base class version from your override — to ensure that EndDialog is called — or call EndDialog yourself.

http://msdn2.microsoft.com/en-us/library/as0ewy07(VS.80).aspx

Best Regards,
DeepuAbrahamK
0
Deepu AbrahamR & D Engineering ManagerCommented:
http://support.microsoft.com/kb/99161
This will be useful for you, if you follow Andy's comment.
Best Reagrds,
DeepuAbrahamK
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

AndyAinscowFreelance programmer / ConsultantCommented:
CMyBaseDialog not an option for class wizard to base another dialog on - I just derive another dialog from CDialog and then do a global find/replace in the .h and .cpp to change CDialog to CMyBaseDialog.  Simple and quick to do.
0
SamratAshokCommented:
If I were you, I would not create an additional base class CMyBaseDialog just for the purposes
of preventing default handling of OnOK behaviour.

Try and think of all the other functionalities that you can refactor from all dialog classes into the base
one.  If you have got nothing that can be refactored, avoid creating a base class.

Given that MFC inheritance tree expands in each class, you have no other alternative than to
create an empty implementation of OnOK in each of the derived dialog classes for which you
want the default behaviour of OnOK disabled. You can implement it with just one single line
in the header file, whereas approach of using a base class will need more lines of code
and it does have marginal overhead in speed as well.
0
Deepu AbrahamR & D Engineering ManagerCommented:
>>Try and think of all the other functionalities that you can refactor from all dialog classes into the base one.  
What I would say is, if you know all the functionalities your derived classes are going to do then there is no harm doing that.
0
PriyeshCommented:
If you dont want to touch code or worry about base classes, keep the IDOK button in all your dialogs, change it's visible to false, and disabled to true and it should do what you want.
0
SamratAshokCommented:
Priyesh, DJ and others:
That does not always work. MFC Dialog boxes are overridden to handle Enter and Escape Keys to work as OK or Cancel event respectively. Only reliable way to disable them from being
inadvertently handled is to override OnOK method and not making calls to CDialog::OnOK.
Ditto for OnCancel ...
0
AndyAinscowFreelance programmer / ConsultantCommented:
others !  - see my comment, that is a simple way to avoid code duplication in the OnOK handler, as requested.  
0
SamratAshokCommented:
Andy,

Typically, OnOK Method that will suppress the default behaviour of OnOK will have 0 lines of code.
Hence, I am saying that it is better to duplicate 0 lines of actual code than to create structures to
avoid duplication!!!

And it is even better to implement the whole 0 code line methods in header file only.

0
AndyAinscowFreelance programmer / ConsultantCommented:
I know about the default behaviour.  I am not saying your suggestion is worse.
You just made a blanket statement that the code from the 'others' does not always work.  My suggestion does do what was asked for - it DOES work, period.

Also there can be circumstances where the OnOK (or other function) also does other work (eg. Check if all required information is entered) so having an intermediate class really can reduce code duplication.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.