title: Strange Issue Validating Empty Text Box in Winforms .NET 4.0 VB.NET
body: <p>I am just adding validation into a new winform. I am using the <strong>validating</strong> and <strong>validated</strong> events, in conjunction with an <strong>error provider</strong> control. Please see code below.</p>
  
  <p>No problem with first validation (ie. if user enters more than 12 characters). Error is set in validating event, and error stays until user enters 12 or less characters. <strong>Validated</strong> event is then entered and error is reset to empty string.</p>
  
  <p>However, there is a problem with the 2nd validation on same text box. The code checks for zero length in text box.. and sets error with provider. Watching in the debugger, this code is indeed invoked, and error is set ok. Then something weird happens, the <strong>validated</strong> event is <em>(unexpectedly)</em> fired, and the code there promptly resets the error. </p>
  
  <p>Why is the <strong>validated</strong> event fired when there is already an error set ? This seems like a .NET bug. Can anyone suggest another way of doing this, or say what I am doing wrong ?</p>
  
  <p>Thanks,</p>
  
  <p>Bazza</p>
  
  <pre><code>Private Sub SampleCodeTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles SampleCodeTextBox.Validating
      If SampleCodeTextBox.Text.Length &gt; 12 Then
          e.Cancel = True
          FormErrorProvider.SetError(SampleCodeTextBox, "Max of 12 characters for sample code")
      End If
  
      If SampleCodeTextBox.Text.Trim.Length = 0 Then
          FormErrorProvider.SetError(SampleCodeTextBox, "Must enter a sample code")
      End If
  End Sub
  
  Private Sub SampleCodeTextBox_Validated(sender As Object, e As System.EventArgs) Handles SampleCodeTextBox.Validated
      FormErrorProvider.SetError(SampleCodeTextBox, "")
  End Sub
  </code></pre>