Solved

Implementing OnOK() in base CDialog class

Posted on 2007-03-30
12
408 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 44

Accepted Solution

by:
AndyAinscow earned 500 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:DeepuAbrahamK
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:DeepuAbrahamK
ID: 18828279
http://support.microsoft.com/kb/99161
This will be useful for you, if you follow Andy's comment.
Best Reagrds,
DeepuAbrahamK
0
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!

 
LVL 44

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:DeepuAbrahamK
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 44

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 44

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

730 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