Solved

Disable termination of a dialog window by Enter key

Posted on 1998-10-16
5
502 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
[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
5 Comments
 
LVL 1

Expert Comment

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

Accepted Solution

by:
plaroche earned 20 total points
ID: 1323441
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
ID: 1323442
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
ID: 1323443
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
ID: 1323444
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

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!

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

728 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