Solved

How to use ENTER like TAB on a CEdit Ctrl?

Posted on 2001-07-08
4
678 Views
Last Modified: 2013-11-20
Hi,

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

MiKo93
0
Comment
Question by:MiKo93
[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
4 Comments
 
LVL 12

Accepted Solution

by:
migel earned 100 total points
ID: 6263017
Hi!
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->SetFocus();
            pWndNext->SendMessage(EM_SETSEL, 0, -1);
            return;
            }

        pWndNext = pWndNext->GetWindow(GW_HWNDNEXT);
        }
    }
}
// default way:
CDialog::OnOK();
}
0
 
LVL 2

Expert Comment

by:GloriousRain
ID: 6263600
<MSDN>
 ////////////////////////////////////////////////////////////////
 // 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__;
 #endif
 
 //////////////////
 // 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?
 public:
    CTabDialog();
    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);
    DECLARE_MESSAGE_MAP()
 };
 
 ////////////////////////////////////////////////////////////////
 // Application class
 //
 class CApp : public CWinApp {
 public:
    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
 //
 BEGIN_MESSAGE_MAP(CTabDialog, CDialog)
    ON_MESSAGE(WM_KICKIDLE,       OnKickIdle)
    ON_COMMAND(ID_CHANGE_TABS,    OnChangeTabs)
    ON_COMMAND(ID_RETURN,         OnReturn)
    ON_COMMAND_EX(ID_NEXT_FIELD,  OnNextPrevField)
    ON_COMMAND_EX(ID_PREV_FIELD,  OnNextPrevField)
    ON_UPDATE_COMMAND_UI(ID_CHANGE_TABS, OnUpdateChangeTabs)
 END_MESSAGE_MAP()
 
 //////////////////
 // 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
       ASSERT(m_hAccel);
       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(),
       MAKEINTRESOURCE(IDD_DIALOG1));
    ASSERT(m_hAccel);
    GotoDlgCtrl(GetDlgItem(IDC_EDIT1));
    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()
 {
    OnNextPrevField(ID_NEXT_FIELD);
 }
 
 /////////////////
 // Go to next or previous field
 //
 BOOL CTabDialog::OnNextPrevField(UINT nCmdID)
 {
    CWnd* pWnd = CWnd::GetFocus();
    if (m_bRandomTabOrder) {
       static int controls[] = { IDC_EDIT1,
          IDC_EDIT2, IDC_EDIT3, IDC_COMBO1, ID_CHANGE_TABS, IDOK, IDCANCEL };
       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);
 
    ASSERT(pWnd);
    GotoDlgCtrl(pWnd);
    return TRUE;
 }
<MSDN>
0
 
LVL 8

Expert Comment

by:VinExpert
ID: 6264358
Hi,

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.
VinExpert
0
 

Author Comment

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

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

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

Thanks to all of you
MiKo93
             
0

Featured Post

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

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

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: 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.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

627 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