Visual Studio MFC change text in Edit Control while typing/dynamically

daktaklakpak 41 Reputation points
2020-12-03T23:45:56.063+00:00

Hello,

I am trying to set up a MFC C++ App in Visual Studio 2019 such that if the user turns on a 'Letters to Numbers' mode via a radio button, the letters in their text change AS THEY TYPE to numbers.

void Onsomebtnclick()
{
//convert CString to String of m_inputString
//do some string manipulation
//convert back to CString
//SetDlgItemText(txtInputBox, result)
}

Currently, for testing I can see how it would work for a button on click, it would do the logic, and just SetDlgItemText of the result. But that would be after they have typed, not WHILE they are typing.

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,198 questions
Visual Studio Debugging
Visual Studio Debugging
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Debugging: The act or process of detecting, locating, and correcting logical or syntactical errors in a program or malfunctions in hardware. In hardware contexts, the term troubleshoot is the term more frequently used, especially if the problem is major.
1,004 questions
Visual Studio Testing
Visual Studio Testing
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Testing: The act or process of applying tests as a means of analysis or diagnosis.
351 questions
0 comments No comments
{count} votes

Accepted answer
  1. Dylan Zhu-MSFT 6,421 Reputation points
    2020-12-04T08:39:40.17+00:00

    Hi daktaklakpak-2506

    Maybe you could use OnEnChangeEdit event to modify your string, and then use DoDataExchange method to update your edit control, which is like:

    void CMFCApplication1Dlg::DoDataExchange(CDataExchange* pDX)  
    {  
        CDialogEx::DoDataExchange(pDX);  
        DDX_Control(pDX, IDC_EDIT1, m_editctrl);  
        if (str) {  
            DDX_Text(pDX, IDC_EDIT1, str);  
        }     
    }  
    
    void CMFCApplication1Dlg::OnEnChangeEdit1()  
    {  
        // TODO:  If this is a RICHEDIT control, the control will not  
        // send this notification unless you override the CDialogEx::OnInitDialog()  
        // function and call CRichEditCtrl().SetEventMask()  
        // with the ENM_CHANGE flag ORed into the mask.  
    
        // TODO:  Add your control notification handler code here  
    
        GetDlgItemText(IDC_EDIT1,str);  
    
        if (str) {  
            CString r = str.Right(1);  
    
            if (r == 'I' || r == 'i')  
                str.Replace(r, _T("1"));  
            else if (r == 'E' || r == 'e')  
                str.Replace(r, _T("3"));  
            else if (r == 'O' || r == 'o')  
                str.Replace(r, _T("0"));  
    
            UpdateData(false);  
            len = m_editctrl.GetWindowTextLengthW();    
            m_editctrl.SetSel(len, len,false);    //Move the cursor to the end of the text  
            m_editctrl.SetFocus();  
        }  
    }  
    

    Hope it could help you.

    Best Regards,
    Dylan


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our **documentation to enable e-mail notifications if you want to receive the related email notification for this thread.**

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. RLWA32 45,701 Reputation points
    2020-12-04T01:46:45.39+00:00

    One approach would be to derive your own class from CEdit. In your derived class override PreTranslateMessage to inspect WM_CHAR messages and replace characters based on your defined rules.

    Add a flag that can be checked in your PreTranslateMessage override to turn replacement on or off. Set or clear the flag in the dialog's handlers for the radio button BN_CLICKED notifications.

    0 comments No comments

  2. daktaklakpak 41 Reputation points
    2020-12-04T09:52:09.723+00:00

    @Dylan Zhu-MSFT
    Hello Dylan, I just got something similar to work where the text is changing correctly, but I am a newbie and didn't understand until I read your code that I need to set a Control variable to do the SetSel and SetFocus methods. Your answer was super helpful and got me going. Thanks!

    P.S. I'm guessing I can set multiple variables (of either type VALUE or CONTROL) to any widget in my DLG?

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.