How to use ENTER like TAB on a CEdit Ctrl?

Posted on 2001-07-08
Last Modified: 2013-11-20

I have a modal dlg box with several CEdit controls.

TAB jumps from one control to the next, while pressing ENTER closes the dlg with OnOK (as OK-button is standard selection).

Regular behavior by framework, I think.

When pressing ENTER after typing data into the first CEdit control, I want the focus jump to the next control, like it does when pressing TAB, and not close the dlg.

The user should be able to use the rightern keyboard block to fill all CEdit controls, one after the other.

Hope you understand what I mean...

How do I do that ? I heard that something like message reflection could help.

Thank you

Question by:MiKo93
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
LVL 12

Accepted Solution

migel earned 100 total points
ID: 6263017
May be this solution will helps you:
add OnOK handler in the your dialog:

void CMyDialog::OnOK()
CWnd* pFocus = GetFocus();
if (pFocus)
char szClass[32] = "";
GetClassName(pFocus->GetSafeHwnd(), szClass, 32);
if (!strcmp(szClass, "edit")) // check focus only in the edit boxes
    //try to set entry to the next edit
    // and skip another controls
    CWnd* pWndNext = pFocus->GetWindow(GW_HWNDNEXT);
    while (pWndNext)
        GetClassName(pWndNext->GetSafeHwnd(), szClass, 32);
        if (!strcmp(szClass, "edit")) // got it!
            // also you can check here enabled/disabled state of the edit
            pWndNext->SendMessage(EM_SETSEL, 0, -1);

        pWndNext = pWndNext->GetWindow(GW_HWNDNEXT);
// default way:

Expert Comment

ID: 6263600
 // TabDlg 1997 Microsoft Systems Journal.
 // If this program works, it was written by Paul DiLascia.
 // If not, I don't know who wrote it.
 // TabDlg illustrates how to make a dialog with dynamic TAB order
 // and that handles the RETURN key.
 #include <afxwin.h>         // MFC core and standard components
 #include <afxext.h>         // MFC extensions
 #include <afxpriv.h>        // for WM_KICKIDLE
 #include <stdlib.h>         // for random number generator fns
 #include "resource.h"
 #ifdef _DEBUG
 #define new DEBUG_NEW
 #undef THIS_FILE
 static char THIS_FILE[] = __FILE__;
 // Dialog class that alters the TAB sequence and handles
 // the RETURN key.
 class CTabDialog : public CDialog {
    HACCEL m_hAccel;                     // dialog accelerators
    BOOL   m_bRandomTabOrder;            // do random TAB order?
    virtual BOOL OnInitDialog();
    virtual BOOL PreTranslateMessage(MSG* pMsg);
    // Message/command handlers
    afx_msg LRESULT OnKickIdle(WPARAM wp, LPARAM lp);
    afx_msg void OnChangeTabs();
    afx_msg void OnReturn();
    afx_msg void OnUpdateChangeTabs(CCmdUI* pCmdUI);
    afx_msg BOOL OnNextPrevField(UINT nCmdID);
 // Application class
 class CApp : public CWinApp {
    CApp() { }
    virtual BOOL InitInstance();
 } theApp;
 // Initialize: just run the dialog and quit.
 BOOL CApp::InitInstance()
    srand(time(NULL)); // seed random number generator
    CTabDialog dlg;
    int nResponse = dlg.DoModal();
    if (nResponse == IDOK) {
       // TODO: handle case when the dialog is dismissed with OK
    } else if (nResponse == IDCANCEL) {
       // TODO: handle case when the dialog is dismissed with Cancel
    // Since the dialog has been closed, return FALSE so that we exit the
    // application, rather than start the application's message pump.
    return FALSE;
 // CTabDialog
    ON_MESSAGE(WM_KICKIDLE,       OnKickIdle)
    ON_COMMAND(ID_RETURN,         OnReturn)
 // Construct dialog: set everything to zero or NULL.
 CTabDialog::CTabDialog() : CDialog(IDD_DIALOG1)
    m_hAccel = NULL;
    m_bRandomTabOrder = FALSE;
 // Possibly translate accelerator key.
 BOOL CTabDialog::PreTranslateMessage(MSG* pMsg)
    if (pMsg->message >= WM_KEYFIRST && pMsg->message <= WM_KEYLAST) {
       // Translate the message using accelerator table
       return ::TranslateAccelerator(m_hWnd, m_hAccel, pMsg);
    return CDialog::PreTranslateMessage(pMsg);
 // Handle idle message: update dialog controls. This is required to make
 // the idle update command UI mechanism work for a dialog.
 LRESULT CTabDialog::OnKickIdle(WPARAM wp, LPARAM lCount)
    UpdateDialogControls(this, TRUE);
    return 0;
 // Initialize dialog: load accelerators and set initial focus.
 BOOL CTabDialog::OnInitDialog()
    m_hAccel = ::LoadAccelerators(AfxGetInstanceHandle(),
    return FALSE; // because I have set the focus
 // Scramble/unscramble tab order
 void CTabDialog::OnChangeTabs()
    m_bRandomTabOrder = !m_bRandomTabOrder;
 // Update name of Scramble/unscramble button
 void CTabDialog::OnUpdateChangeTabs(CCmdUI* pCmdUI)
    pCmdUI->SetText(m_bRandomTabOrder ?
       "Unscramble &TAB Order" : "Scramble &TAB Order");
 // Handle Return (Enter) key
 void CTabDialog::OnReturn()
 // Go to next or previous field
 BOOL CTabDialog::OnNextPrevField(UINT nCmdID)
    CWnd* pWnd = CWnd::GetFocus();
    if (m_bRandomTabOrder) {
       static int controls[] = { IDC_EDIT1,
       const NCONTROLS = sizeof(controls)/sizeof(int);
       // Select a random control that's different from the current one.
       int nIDCtrl;
       while ((nIDCtrl = controls[rand() % NCONTROLS]) == pWnd->GetDlgCtrlID())
          ; // same as current, try another
       pWnd = GetDlgItem(nIDCtrl);
    } else
       // Normal next/prev using dialog's TAB order.
       pWnd = GetNextDlgTabItem(pWnd, nCmdID==ID_PREV_FIELD);
    return TRUE;

Expert Comment

ID: 6264358

U can achive this functionality in a simple way. Use classwizard and map PreTranslateMessage() to ur dialog which has the edit controls on it. Then modify the message as shown bellow to send the tab key itself, so that, by default nature it will behave as u wish!!!

BOOL CSendTabDlg::PreTranslateMessage(MSG* pMsg)
     if(     (pMsg->message == WM_KEYDOWN) &&
          (pMsg->wParam == VK_RETURN))
          pMsg->wParam = VK_TAB;
     return CDialog::PreTranslateMessage(pMsg);

Try it out.

Author Comment

ID: 6266140
Great ! It worked like you placed it. I only had to change the "edit" into "Edit" ;-))

Okay, okay, I SHOULD have searched MSDN better...

You definitely anwered my question. I just liked the option to "exclude" all non-Edit controls by migel.

Thanks to all of you

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
repeatEnd java challenge 42 85
iSeries FTP Exit Program 8 173
Unix Command -- Challenging  question 7 106
Excel file not created as expected 7 110
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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.
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

738 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