Detecting CR in an Edit Box

Posted on 2004-11-12
Last Modified: 2013-11-20
I have an edit box that gets a decimal value from the user.  I pick up the value with an UpdateData() in the routine for a pressed button in the same dialog box, and this works fine.  But if the user enters carriage-return after entering the  decimal input, the entire dialog box closes down and control exits my program. I thought I might be able to fix this by putting code in the EN_CHANGE routine for the edit box to look for CR, but this routine only gets called when a non-CR character is entered. CR does the same thing as before - closes down the  diaglog box and exits.

So how can I determine that CR was entered into my edit box?  

Question by:steva
    LVL 5

    Assisted Solution

    Add PretranslateMessage() as it follows:

    BOOL CMyDlg::PreTranslateMessage(MSG* pMsg)
          // TODO: Add your specialized code here and/or call the base class
    if(pMsg->message == WM_KEYDOWN)
          long nID = ::GetDlgCtrlID(pMsg->hwnd);
          if(nID== IDC_EDIT1){ //ID of desired editbox
          if(pMsg->wParam == VK_RETURN)
                return TRUE;//Or anything you want


    Good Luck!

    Author Comment

    Ok, this looks good.  But where is it called from, and where do I get the pMsg to pass?

    LVL 4

    Accepted Solution

    PreTranslateMessage is event driven, so you don't need to call it directly from another function. In VC++ just press ctrl+w, and then select the dialog class that contains your edit box control, then select "PreTranslateMessage" from the "messages" list. Then click add function and edit the function body to fit the function given above.

    Author Comment

    That works fine.  Now when PretranslateMessage() detects CR entered in the edit box it just calls call the button routine that normally picked up the value.

    Thanks for the help.  I split the points.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    if loop error 4 69
    countEvens challenge 2 48
    matchUp  challenge 9 51
    How to convert MFC::CString to UTF8 wchar_t* 10 48
    Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
    If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
    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 ( 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…

    746 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

    13 Experts available now in Live!

    Get 1:1 Help Now