Solved

Disable termination of a dialog window by Enter key

Posted on 1998-10-16
5
489 Views
Last Modified: 2013-11-20
I'm programming a dialog window using VC5++ MFC. The dialog window has many items (buttons, sliders, checkboxes, comboboxes, etc.) and of course an OK and a CANCEL button. The program runs fine except that it terminates when the user hits Enter key, even if any other items (not OK or CANCEL)is highlighted. For example, in a combobox, the user wants to choose one selection and hits Enter key, then dialog window is gone! How do I solve the problem? Thanks.
0
Comment
Question by:tsauy
5 Comments
 
LVL 1

Expert Comment

by:Grailman
Comment Utility
Check out my MFC question on SetFocus() - still open
0
 
LVL 3

Accepted Solution

by:
plaroche earned 20 total points
Comment Utility
ENTER key is translated to the OnOK() function in your dialog, ESCAPE key is translated to OnCancel().

Your example is flawed I think. When the combobox has a list showing, the user can select the item by pressing the ENTER key and it won't close the dialog since it's the combobx that's getting the enter. BUT, if the combo is not showing the list then the ENTER key will close the dialog via OnOK.

In your OnOK override do a GetFocus(), compare what you get with the OK button. When you don't want the dialog to close just return, without calling the base class. For example:


CMyDialog::OnOK()
{
  if( GetFocus() != GetDlgItem(IDOK) ) {
    return;
  }

  CDialog::OnOK();
}
0
 

Author Comment

by:tsauy
Comment Utility
Thanks plaroche. And now it works. I didn't explain the example clearly. I mean to type a new value in the combobox then press Enter...

0
 
LVL 1

Expert Comment

by:Grailman
Comment Utility
plaroche:
Good solution but I've got a question in relation to it. If you use the same technique for IDCANCEL, it will disable the system menu 'X'. How can this be left enabled?
0
 

Expert Comment

by:thinh
Comment Utility
You can use this in OnCommand message handler

      int wID = LOWORD(wParam);         // item, control, or
      switch(wID)
      {
         .....
      case IDOK:
            return 0; // ignore the Enter key
         .....
        }

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

15 Experts available now in Live!

Get 1:1 Help Now