Hi Everyone,

In my form there is a "Validating" event on a textbox.

This form also has a "Cancel" button.

Is there a way to not execute the "Validating" event when the user clicks on the "Cancel" button?

Please show me code to do this.

Thanks.

Truly,
Emad

Dani AI

Generated

The built-in way to skip a control’s Validating when the user clicks Cancel is to turn off validation on the Cancel button itself. That is safer than toggling a flag in MouseEnter/Leave as showed, because it also covers keyboard focus (Tab/Shift+Tab) and pressing Esc.

Example (Windows Forms, VB.NET):

' In designer or at startup:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    btnCancel.CausesValidation = False   ' do not trigger Validating when Cancel gets focus/clicked
    Me.CancelButton = btnCancel          ' Esc maps to Cancel
End Sub

Private Sub TextBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
    If String.IsNullOrWhiteSpace(TextBox1.Text) Then
        e.Cancel = True                  ' keep focus here when invalid
        ErrorProvider1.SetError(TextBox1, "Required.")
    Else
        ErrorProvider1.SetError(TextBox1, "")
    End If
End Sub

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    If Me.ValidateChildren() Then        ' explicitly run validation when saving
        ' proceed with save...
    End If
End Sub

Notes:

  • Only set CausesValidation = False on controls that should bypass validation (typically just Cancel). Everything else should remain True.
  • If you prefer not to block focus when invalid, set the form’s AutoValidate = EnableAllowFocusChange and still keep Cancel’s CausesValidation = False.
  • For ASP.NET Web Forms, the equivalent is to set the Cancel button to CausesValidation="false":
    <asp:Button ID="btnCancel" runat="server" Text="Cancel" CausesValidation="false" />

This keeps your validation logic simple and avoids edge cases where the user never hovers the button but still triggers Validating.

Recommended Answers

All 2 Replies

See if this helps for bypassing the Validation of a TextBox.
1 TextBox, 1 Button

Public Class Form1
    Private bValidateTextBox As Boolean = True

    Private Sub Button1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter
        bValidateTextBox = False '// Disable Validation.
    End Sub

    Private Sub Button1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseLeave
        bValidateTextBox = True '// Enable Validation.
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '// do stuff here.
        MsgBox("TextBox Validation was Canceled") '// for testing.
    End Sub

    Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
        If bValidateTextBox Then
            '// validating code here.
            MsgBox(TextBox1.Text) '// for testing.
        End If
    End Sub
End Class
commented: CodeOrder was very helpful. +1

Hi CodeOrder,

Thanks for your help. It works very well.

Truly,
Emad

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.