Solved

Closing of dialog-based application

Posted on 2002-06-04
1
193 Views
Last Modified: 2013-11-20
My dialog-based MFC application is automatically closed whenever I hit the ENTER key even I take out default OK and Cancel buttons and don't respond to the event sent to the dialog when ENTER key is hit. Can anyone help ?
0
Comment
Question by:kevinnguyen
1 Comment
 
LVL 3

Accepted Solution

by:
Crius earned 55 total points
ID: 7054698
How to Disable Default Pushbutton Handling for MFC Dialog

Q122489


--------------------------------------------------------------------------------
The information in this article applies to:

The Microsoft Foundation Classes (MFC), included with:
Microsoft Visual C++ for Windows, 16-bit edition, versions 1.0, 1.5
Microsoft Visual C++, 32-bit Editions, versions 1.0, 2.0, 4.0

--------------------------------------------------------------------------------


SUMMARY
Windows dialog boxes support the use of a default button (pushbutton) that is automatically activated when the ENTER key is pressed while focus is not on a command button. The MFC class library will automatically call the CDialog::OnOK() virtual function for a dialog whenever the default button (pushbutton) is activated by pressing the ENTER key.

This behavior conforms to the guidelines set forth by "The Windows Interface: An Application Design Guide." This manual is included with the Windows version 3.1 SDK documentation. It is also available from Microsoft Press.



MORE INFORMATION
Although default button (pushbutton) support is recommended, you might want to disable or modify the standard implementation in certain situations. You can do this in an MFC application by following these steps:



Load the dialog into App Studio and change the OK button identifier from IDOK to something else such as IDC_MYOK. Also, clear the check from Default Button property.


Use ClassWizard to create a message handling function for this button named OnClickedMyOK. This function will be executed when a BN_CLICKED message is received from this button.


In the code for OnClickedMyOK, call the base class version of the OnOK function. Here is an example:

   void CMyDialog::OnClickedMyOK()
   {
      CDialog::OnOK();
   }



Override OnOK for your dialog, and do nothing inside the function. Here is an example:

   void CMyDialog::OnOK()
   {
   }



Run the program and bring up the dialog. Give focus to a control other than the OK button. Press the RETURN key. Notice that CDialog::OnOK() is never executed.


This technique effectively disables the standard default button (pushbutton) support in an MFC program. The OK button will continue to behave as before when activated by the mouse or through a mnemonic keystroke, but the ENTER key will not trigger OnOK handling.

This same technique can be used to disable or modify default OnCancel processing. The escape key generates a call to CDialog::OnCancel just as the enter key generates an OnOK function call.

Additional query words: kbinf 1.00 1.50 2.00 2.10 2.50 3.00 4.00

Keywords :
Issue type :
Technology : kbAudDeveloper kbMFC

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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: 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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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

13 Experts available now in Live!

Get 1:1 Help Now