Solved

How to use ENTER like TAB on a CEdit Ctrl?

Posted on 2001-07-08
4
672 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
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

706 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

14 Experts available now in Live!

Get 1:1 Help Now