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
Solved

Implementing OnOK() in base CDialog class

Posted on 2007-03-30
12
406 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
  • 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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 video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

840 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