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

Platform: WinForms| Category: Controls

Say textBox1 and cancelButton 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

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.