Form validation with ErrorProvider and event validation

I am new to verification. I have a winform C # project that I want to validate the form before closing. However, I want this check to be performed when I click the button. Therefore, I have an event that fires for this like this:

if (!this.ValidateChildren())
{
   MessageBox.Show("Validation failed");
}
else
{
   MessageBox.Show("Validation passed with flying colours. :)");
   this.Close();
}

I only want to close the form if the verification was successful. Easy enough. However, I do not want the validation to be performed when text fields lose focus, only when the entire form is validated.

Every control that I want to test, I registered a check event. They use "e.Cancel = true"; to cancel the check. I use the ErrorProvider class for visual maintenance.

So, the main question is: what is best suited for checking a specific set of controls only when I want, and not when the focus is lost from the control?

EDIT: Currently, as a job, I have a method that turns the CausesValidation property on and off. I default everything to prevent CauseValidation, letting them all before using this event to validate the entire form and turn them off again.

I really do not see this as an ideal approach. Are there any other “elegant” solutions?

+3
source share
6 answers

, , , ,

form.Autovalidate = Disable

form.Autovalidate = EnableAllowFocusChange
+1

- :

1) , - .

2) , , , , / . , , .

3) , ErrorProvider SetError .

4) , (DialogResult = OK) (DialogResult = None).

, ErrorProvider .

0
    private void TxtNama_Validating(object sender, CancelEventArgs e)
    {
        e.Cancel = string.IsNullOrEmpty(TxtNama.Text);
        this.errorProvider1.SetError(TxtNama, "Nama Barang Harus Diisi..!!");            
    }

    private void CmdSave_Click(object sender, EventArgs e)
    {
        if(this.ValidateChildren(ValidationConstraints.Enabled))
        {
            MessageBox.Show("Simpan Data Barang..?" , "SIMPAN DATA", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
        }
    }
0

, errorProvider . , object.ValidateChildren() false, .

, CancelEventargs.Cancel true; , , , , - .

    Form1.Autovalide = Disabled

    private void textBox2_Validating(object sender, CancelEventArgs e)
    {
       if (textBox2.Text.Length == 0)
       {
          errorProvider1.SetError(textBox2, "Must provide an entry.");
          e.Cancel = true;
       } else {
          errorProvider1.SetError(textbox2, "");
       }
    }

    private void button1_click(object sender, EventArgs e)
    {
       if (this.ValidateChildren(ValidationConstraints.Enabled | ValidationConstraints.ImmediateChildren))
       {
          MessageBox.Show("All well.", "Valid", MessageBoxButtons.OK);
       }  else {
          MessageBox.Show("All is ruin and woe!", "Invalid", MessageBoxButtons.OK);
       }
}
0

, . .

MouseEnter MouseLeave

private void btnCancel_MouseEnter(object sender, EventArgs e)
        {
            AutoValidate = AutoValidate.Disable;
        }

private void btnCancel_MouseLeave(object sender, EventArgs e)
        {
            AutoValidate = AutoValidate.EnablePreventFocusChange;
        }

This will disable the check when entering the button to click, and then clicking on the “Cancel” button, you can click the “Event” button, and I can execute any logic that is needed there, including error clearing.

0
source

All Articles