Serialcoder en Français Serialcoder in English
TEL : +33 (0)9 72 13 15 17

Windows Forms FAQ resources

2. Windows Forms Controls

2.16 I would like to prevent validation in my textbox when the user clicks on my Cancel button, how do I do this?


Say textBox1 and canelButton and the control names, then this is how you could do this:


[C#]
// Handler to the Validating event of the TextBox.
private void TextBox_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
     if (!this.cancelButton.Focused)
     {
          // Do this only when the cancel button is not clicked.
          if(invalidState)
               e.Cancel = true;
     }
}
[VB.Net]
' Handler to the Validating event of the TextBox.
Private Sub TextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
     If Not Me.cancelButton.Focused Then
          ' Do this only when the cancel button is not clicked.
          If invalidState Then
               e.Cancel = True
          End If
     End If
End Sub