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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
while loop over for loop 7 93
centeredAverage challenge 8 132
has22 challenge 11 80
Execute multiple curl cmds with sleep and send output to file 10 89
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

862 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now