2

I'm working on a simple WinForms application for a public school where users can identify themselves by entering either their network IDs (which are not protected information) or their system IDs (which are protected information). I want to switch to a password character when the program detects a system ID (which is working just fine); however, when I do this, my application also fires the textbox's Leave event, which tells users to fix a problem with the login data...before there's even a problem.

Here's my code:

void login_TextChanged(object sender, EventArgs e)
{
    login.UseSystemPasswordChar = login.Text.StartsWith(<prefix-goes-here>);
}

private void login_Leave(object sender, EventArgs e)
{
    if (login.Text.StartsWith(<prefix-goes-here>) && login.Text.Length != 9)
    {
        signInError.SetError(login, "Your System ID must be nine digits.");
        login.BackColor = Color.LightPink;
    }
    else if (login.Text.IsNullOrWhiteSpace())
    {
        signInError.SetError(login, "Please enter your username or System ID.");
        login.BackColor = Color.LightPink;
    }
    else
    {
        signInError.SetError(login, string.Empty);
        login.BackColor = Color.White;
    }
}

Ultimately, I don't know that this will cause a ton of problems, and I could move this validation step to the Click event of the sign in button on my form, but I'd rather do validation piece-by-piece if possible.

2
  • It didn't make any sense to me either. I checked the call stack in Visual Studio, and it shows login_TextChanged as having called login_Leave. I've gone through the designer several times to make sure that there's nothing else tied to the event, cleaned, and rebuilt the application as well, but it still happens.
    – jwheron
    Sep 22, 2011 at 18:13
  • Okay, it appears that the critical detail -- which I left out, not knowing it was a critical detail -- is that this TextBox control is inside a GroupBox control. I moved the TextBox outside the GroupBox control, and these events fire as intended. Inside the GroupBox, it acts as I described above.
    – jwheron
    Sep 22, 2011 at 18:24

3 Answers 3

2

Putting the TextBox inside a GroupBox does reproduce that behavior-- which is odd.

If you want to keep your GroupBox, here is a work around:

private void login_TextChanged(object sender, EventArgs e)
{
  login.Leave -= login_Leave;
  login.UseSystemPasswordChar = login.Text.StartsWith(<prefix-goes-here>);
  login.Leave += login_Leave;
}
1

For whatever reason, the Leave event fires when the login TextBox is inside a GroupBox control. Replacing the GroupBox with a simple Label control prevented the code within the TextChanged event from firing the Leave event.

0

Yes, this is a quirk of the UseSystemPasswordChar property. It is a property that must be specified when the native edit control is created (ES_PASSWORD). Changing it requires Winforms to destroy that native control and recreate it. That has side-effects, one of them is that the focus can't stay on the textbox since the window disappears. Windows fires the WM_KILLFOCUS notificaiton.

Being inside a GroupBox is indeed a necessary ingredient, Winforms doesn't suppress the Leave event when it gets the notification. Bug.

Many possible fixes. You could set a flag that the Leave event handler can check to know that it was caused by changing the property.

3
  • No, it is not. The ES_PASSWORD style can be changed at any time by sending EM_SETPASSWORDCHAR, and does not require recreation of the control window.
    – Ben Voigt
    Jan 16, 2015 at 19:06
  • 1
    That has beans to do with the way it was implemented in Winforms and its support for Win98. I don't know anybody that is so eager to show his ignorance repeatedly. Jan 16, 2015 at 19:14
  • Perhaps WinForms does recreate the control window, but MSDN says it isn't required. Your answer uses the words requires and must, which are inaccurate (at least according to the Win32 docs). And other online references that still cover Win95 indicate that EM_SETPASSWORDCHAR was supported on those OSes also.
    – Ben Voigt
    Jan 16, 2015 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.