[Solved] Check if there were any errors during validation [closed]


If you’re using an error provider and handling onvalidating, it should look like this:

    private void textBox1_Validating(object sender, CancelEventArgs e)
    {
        TextBox tb = sender as TextBox;
        if (tb.Text.Length > 0)
        {
            e.Cancel = true;
            errorProvider1.SetError(tb, "Please leave this textbox blank!");
        }
        else
        {
            errorProvider1.SetError(tb, "");
        }
    }

This will prevent you clicking off the control

Alternatively, implement the ok button click handler like this:

    private void OkBtn_Click(object sender, EventArgs e)
    {
        if (!Validate())
        {
            DialogResult = System.Windows.Forms.DialogResult.None;
        }

    }

The “DialogResult = none” business is what stops the form closing.

11

solved Check if there were any errors during validation [closed]