Solved

Disable termination of a dialog window by Enter key

Posted on 1998-10-16
5
495 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
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

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‚Ķ
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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 use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

773 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