Solved

Enter key doesn't work

Posted on 1998-08-14
10
244 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Different colored text in ComboBox without Subclassing 8 60
Strange date stored 22 121
Expand to include initial dialog with two choices. 9 77
Problem to App source 6 39
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: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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 tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

770 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