简体   繁体   中英

MessageBox Within A WinForms TextBox Validating Event Handler

I have a case where a textbox is validated to make sure it is not blank. However, there are some edge cases where the value should actually be blank. A Nullable<bool> blankText = null variable is set in the constructor. I'm using this code to validate and double check if the value is actually OK to be blank by confirming this with the user:

private void text_Validating(object sender, CancelEventArgs e)
{
        if (string.IsNullOrWhiteSpace(text.Text))
        {
            do
            {
                if (blankText.HasValue && !blankText.Value)
                {

                    errorProvider.SetError(text, "Blank or whitespace!");
                    e.Cancel = true;
                    break;
                }
                else if (blankText.HasValue && BlankText.Value)
                {
                    errorProvider.SetError(text, "");
                    e.Cancel = false;
                    break;
                }
                else
                {
                    DialogResult result = MessageBox.Show(this, "Field is blank, or contains only whitespace.\nAre you sure you want to use a blank/whitespace?", String.Empty, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (result == DialogResult.Yes)
                        blankText = true;
                    else
                        blankText = false;
                }
            }
            while (true);
        }
        else
        {
            e.Cancel = false;
            errorProvider.SetError(text, "");
        }
    }

Even if the value for blankText = null it will still set the errorProvider error and fail validation. The MessageBox dialog is never displayed. I know that according to the documentation Microsoft states the following:

Do not attempt to set focus from within the Enter, GotFocus, Leave, LostFocus, Validating, or Validated event handlers. Doing so can cause your application or the operating system to stop responding. Source

Obviously when the MessageBox is displayed the control will implicitly lose focus...so maybe that's why this strange behavior is occurring. Can anyone confirm this? Any suggestions on a better way to handle this case?

In response to @adriano-repetti comment and some additional testing I removed the prompt from the validation event. The full solution I used was to create an additional property of bool? that can be checked if blank values are allowed, disallowed, or undefined. If either disallowed or undefined the validation fails if the value is blank. If the validation fails due to a blank value and the new IsBlankValueAllowed property a prompt is displayed to ask the user to confirm this behavior. I decided to use a Property instead of data within a Tag property since it felt less 'hacked'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM