Solved

Disable termination of a dialog window by Enter key

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add content to output file 4 67
Perl Awk Need Help 3 129
Expand data scrubbing tool 13 35
ffmpeg - "rtsp://...... Operation not permitted" 4 86
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

830 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