Solved

Implementing OnOK() in base CDialog class

Posted on 2007-03-30
12
402 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 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

17 Experts available now in Live!

Get 1:1 Help Now