c++eventsmfceditcontrol

Validating an Edit Control's text while the user types MFC


Question/Problem: I have an edit control (text box) that the user enters a username into. I am attempting to compare the username entered to the ones listed in my List Control. If the username given matches, my button's text should change from Create User to Update User.

My problem is in finding the correct event/time to compare the strings, without creating an infinite loop.

What I have tried: I have tried using the edit control events EN_CHANGE and EN_UPDATE. Both of these events cause stack-overflow exception or an infinite loop. I thought that one of these events would be called every time something is typed or the backspace was used within my edit control.

In my EN_CHANGE / EN_UPDATE event, I compare the username strings and set the button's text. With either event, it is called infinite times:

void Users::OnEnUpdateLoginName()  //EN_UPDATE Event
{
    bool match = false;

    //Compare the edit control text with each List Control text.
    for(int i = 0; i<m_UserList.GetItemCount(); i++)
    {
        if(strcmp(m_UserList.GetItemText(i,0),m_loginName)==0)
            match = true;
    }

    //If the usernames match, change the button's text to "Update User"
    if(match) 
    {
        CWnd *currentSelection = GetDlgItem(TXTC_LOGIN_NAME);
        currentSelection->SetWindowTextA("Update User");
    }
    else
    {
        CWnd *currentSelection = GetDlgItem(TXTC_LOGIN_NAME);
        currentSelection->SetWindowTextA("Create User");
    }
}

example edit control.

If the text in red matches, change the text of the button highlighted in blue.

Should I be using a different event to validate the string in real-time as the user types?


Solution

  • My code had two issues. I needed to use UpdateData, so that data for all my dialog controls would be current. I also was updating the wrong variables. Thanks @rrirower