Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Implementing OnOK() in base CDialog class

Posted on 2007-03-30
12
Medium Priority
?
419 Views
Last Modified: 2013-11-20
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
0
Comment
Question by:DJ_AM_Juicebox
[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
  • 4
  • 3
  • 3
  • +1
12 Comments
 
LVL 45

Accepted Solution

by:
AndyAinscow earned 2000 total points
ID: 18827899
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
 
LVL 11

Expert Comment

by:Deepu Abraham
ID: 18828262
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
 
LVL 11

Expert Comment

by:Deepu Abraham
ID: 18828279
http://support.microsoft.com/kb/99161
This will be useful for you, if you follow Andy's comment.
Best Reagrds,
DeepuAbrahamK
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 45

Expert Comment

by:AndyAinscow
ID: 18828553
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
 
LVL 2

Expert Comment

by:SamratAshok
ID: 18828563
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
 
LVL 11

Expert Comment

by:Deepu Abraham
ID: 18828728
>>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
 
LVL 9

Expert Comment

by:Priyesh
ID: 18828965
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
 
LVL 2

Expert Comment

by:SamratAshok
ID: 18833674
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
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 18834581
others !  - see my comment, that is a simple way to avoid code duplication in the OnOK handler, as requested.  
0
 
LVL 2

Expert Comment

by:SamratAshok
ID: 18834719
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
 
LVL 45

Expert Comment

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
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…
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

636 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