Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Disable termination of a dialog window by Enter key

Posted on 1998-10-16
5
Medium Priority
?
509 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 80 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

598 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