Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Need help extracting data from an edit box.

FlackFlack Member Posts: 88

I am new to mfc programming and I am working on a very simple program. All I have is an edit box. I want the user to enter a number and then I update the variable that represents that number. However, I cant seem to get what I want. The id of the edit box is IDC_MSG. The variable attached to it is m_strMessage of type int.

I want the variable to update whenever the user enters a new value and then hits the enter key. I am not sure which message to use. I tried EN_CHANGE and EN_UPDATE and placed UpdateData(TRUE) in those functions.
What happened though is the the function was called any time the data was changed (after each keystroke), when I want it to work only after the enter key is hit.
(If the function is called any time the data is changed, the check to see if the int is in the proper min-max range occurs each time and that messe things up).

If anything is unclear please let me know and I will do my best to clarify it.

Thanx for the help,


  • FDracheFDrache Member Posts: 64
    Dear Flack,

    I have made the same experiences. MFC has no component which reacts on the Enter key to input something - they have forgotten it. (Delphi is slightly better off.) People seem so used to it that I haven't found any C++ component which would do that.

    Instead, I have only had success with the OnKillFocus event. Yet, this doesn't come when you press Enter, but when you leave the field, for example with the Tab key. Therefore, many programmers convert the Enter key to a Tab key! There are several methods to do this.

    To be able to use the Enter key at all, it is necessary that it does not act on the default button to close the dialog. Actually, there should not be a default button, except it fulfills your need.

    How I have done it: I have overwritten the OnOK function, and then I make a switch-case evaluation depending on which dialog item I am:
    int SteuerID = 0;
    CWnd* pFocusWind;

    pFocusWind = GetFocus();
    if (pFocusWind > 0) SteuerID = pFocusWind->GetDlgCtrlID();

    switch (SteuerID)
    // And so on with other fields

    Maybe other people have found a better way to circumvent this problem.
    Hope this helps you,

  • TheViceTheVice Member Posts: 8
    Dear Flack,

    I've solved the problem like this:

    In the override of PRETRANSLATEMESSAGE of a subclassed edit control I use this code :

    switch (pMsg->message)
    case WM_KEYDOWN:
    int nKey=(int)pMsg->wParam;
    if (nKey==VK_RETURN ||nKey==VK_TAB)
    BOOL bShift=FALSE;
    EndEdit(); // do some formatting...
    if (nKey==VK_TAB) bShift=IsSHIFTpressed();
    if (!bShift) return GetParent()->SendMessage(WM_TABKEY,(WPARAM)this);
    else return GetParent()->SendMessage(WM_SHIFTTABKEY,(WPARAM)this);

    It simply sends a message (WM_TABKEY = WM_USER + xxx) to the parent window. (Use the UpdateData function in the parentwindow)
    The EndEdit function is optional (I use it to format text).
    The IsShiftPressed is a macro:
    #define IsSHIFTpressed() ( (GetKeyState(VK_SHIFT) & (1 << (sizeof(SHORT)*8-1))) != 0 )

    Hope this gets you on your way..


Sign In or Register to comment.