1

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?

Kyle Williamson
  • 2,251
  • 6
  • 43
  • 75
  • Assuming _m_loginName_ represents the edit control, how do you update it each time a character is entered by the user? I don't see where that happens in your code. Also, what is TXTC_LOGIN_NAME? It's used as if its a button. – rrirower Apr 10 '15 at 18:38
  • I feel dumb, I have been staring at this too long. TXTC_LOGIN_NAME should be replaced with `IDC_BUTTON1` (the button to create/update user. I will see how that effects my code now. I assumed that _m_loginName_ would be updated automatically when the event was called. – Kyle Williamson Apr 10 '15 at 18:46
  • 1
    Effectively, each time you wrote to TXTC_LOGIN_NAME you caused another EN_UPDATE event to occur. Additionally, the member variable _m_loginName_ will not automatically be updated unless you update it through the code. There are a few ways to accomplish that. – rrirower Apr 10 '15 at 18:53
  • would `UpdateData(true)` at the start of my event be one way? – Kyle Williamson Apr 10 '15 at 18:55
  • Yes, but, it will also update data for **all** of the dialog controls, not, just _m_loginName_. That's not necessarily a bad thing. – rrirower Apr 10 '15 at 18:57
  • Thank you @rrirower . Using `UpdateData(true)` and the correct button name fixed my problems. – Kyle Williamson Apr 13 '15 at 12:49

1 Answers1

1

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

Kyle Williamson
  • 2,251
  • 6
  • 43
  • 75