0

I want to implement simple validation of user input inside my form.

I have errorProvider1 and txtCode form field which I want to validate. So I've put following

private void txtCode_Validating(object sender, CancelEventArgs e)
        {
            if (txtCode.Text == "")
            {
                e.Cancel = true;
                errorProvider1.SetError(txtCode, "Field cannot be empty");
            }
            else
            {
                errorProvider1.SetError(txtCode,"");
            }

        }

I don't know how to call this method txtCode_Validating when user click on Ok button?

panjo
  • 3,467
  • 11
  • 48
  • 82

1 Answers1

1

You can use something like this

private void btnOK_Click(object sender, System.EventArgs e)
{
   foreach (Control control in this.Controls)
   {
    // Set focus on control
    control.Focus();
    // Validate causes the control's Validating event to be fired,
    // if CausesValidation is True
    if (!Validate())
    {
        DialogResult = DialogResult.None;
        return;
    }
   }
}

Hope it helps

Rajeev Bera
  • 2,021
  • 1
  • 16
  • 30