Solved

Enter key doesn't work

Posted on 1998-08-14
10
245 Views
Last Modified: 2013-11-20
After adding a tab control to my dialog app, I find that if I tab to a button on one of the child windows, the Enter key doesn't send a command message to the main window.  The spacebar does.  If I press enter while the focus is on a control on the main window, the program dies.  Please help.
0
Comment
Question by:duncan2
  • 8
  • 2
10 Comments
 
LVL 6

Accepted Solution

by:
thresher_shark earned 100 total points
ID: 1320839
Answer coming...
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1320840
The program dies because the enter key is interpreted as pressing the default button in the dialog box.  The spacebar is the key that sends commands to the currently selected item in the dialog box.
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1320841
There will always be a default button, even if the box that says "Default button" is unchecked.  The default button check box is useful for overriding the default.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 6

Expert Comment

by:thresher_shark
ID: 1320842
In fact, even if there are no buttons in the app, the enter key will still close it, just as if there were.
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1320843
To prevent the enter key from doing anything, delete the default OK button, and put in your own.

Then go into class wizard, and add a function to handle what happens when the button is pressed.  In the function put in whatever code you had in the okay button that was already there.

But don't call the CDialog::OnOK member function.

After this your code will look something like:
void CYourDlg::OnNewOk()
{ // Your code here.

  // CDialog::OnOK ();
}

Then set this new button as the default button.  The enter key will now push your new button, not terminate the app.
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1320844
Now, if there were never any buttons in your app to begin with, simply override the CDialog::OnOK member function.  This new function should do nothing, i.e. it should look like this:

void CYourDlg::OnOK()
{ // Nothing here.
}

When you press the enter key, it will do nothing.
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1320845
If you have any questions about how to implement any of the above, or any other questions about anything, please ask.

Thank you.
0
 

Author Comment

by:duncan2
ID: 1320846
I don't have any buttons on the main dialog.  There used to be some buttons, so I guess that's why the enter key worked before.  It always activated either the default button or the button with the input focus.  Now it doesn't have a button, so it's closing the program.

If I override CDialog::OnOK, that may fix the problem of killing the program, but I don't think it will fix my other problem.  I have moved the buttons that were on the main dialog to the tab control's child windows.  When I use the Tab key to put the input focus on one of these buttons, pressing Enter should activate that button.  I have handlers for these buttons in the child dialog's class.  These handlers get called if I press the space bar, but not if I press Enter.  In a normal dialog (not a child), if the focus is on a button, pressing Enter will activate that button.  If you can solve this problem, It would be worth another 100 points to me.  Thanks for your help.
0
 

Author Comment

by:duncan2
ID: 1320847
I found an answer to my problem.  It turns out that when Enter is pressed while a button on a child window has focus, a message is sent to the main window instead of the child window containing the button.  If the spacebar is pressed, a message is sent to the child window.  I had to leave the handlers in the child's class to handle the spacebar, and manually add ON_BN_CLICKED macros to the main window class message map with handlers there as well to take care of the Enter key.

Overriding the OnOK function like you suggested fixed the other problem.  Thanks a lot.
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1320848
Glad I could help :-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

808 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