how to close a winform while a validator is in an error condition

Posted on 2006-06-02
Last Modified: 2008-01-09
Hi all,

I decided to use the Winform validation events with the error providers to validate input. It works fine, but I can't close the form until the input is validated.
Is there a way to still allow a form to be closed while the validating event is still waiting for valid input?

Here's the code so far (for VB.Net in VS2003) - I put it in its entirety to cut and paste to see what I mean:

Public Class frmTest3
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()

        'This call is required by the Windows Form Designer.

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
            End If
        End If
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents ErrorProvider1 As System.Windows.Forms.ErrorProvider
    Friend WithEvents ToolTip1 As System.Windows.Forms.ToolTip
    Friend WithEvents txtIPAddress As System.Windows.Forms.TextBox
    Friend WithEvents btnVerifyIP As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.txtIPAddress = New System.Windows.Forms.TextBox
        Me.ErrorProvider1 = New System.Windows.Forms.ErrorProvider
        Me.btnVerifyIP = New System.Windows.Forms.Button
        Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
        Me.txtIPAddress.Location = New System.Drawing.Point(48, 64)
        Me.txtIPAddress.Name = "txtIPAddress"
        Me.txtIPAddress.Size = New System.Drawing.Size(168, 20)
        Me.txtIPAddress.TabIndex = 0
        Me.txtIPAddress.Text = ""
        Me.ErrorProvider1.ContainerControl = Me
        Me.btnVerifyIP.Location = New System.Drawing.Point(72, 112)
        Me.btnVerifyIP.Name = "btnVerifyIP"
        Me.btnVerifyIP.TabIndex = 1
        Me.btnVerifyIP.Text = "Verify IP"
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Name = "frmTest3"
        Me.Text = "frmTest3"

    End Sub

#End Region

    Private Sub frmTest3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ToolTip1.SetToolTip(txtIPAddress, "enter a zip code here")
    End Sub

    Private Sub txtIPAddress_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtIPAddress.Validating
        If Not IsIPAddress(txtIPAddress.Text) Then
            e.Cancel = True
            txtIPAddress.Select(0, txtIPAddress.Text.Length)
            ErrorProvider1.SetError(txtIPAddress, "Invalid IP address")
        End If
    End Sub

    Private Sub txtIPAddress_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtIPAddress.Validated
        ErrorProvider1.SetError(txtIPAddress, "")
    End Sub

    Private Function IsIPAddress(ByVal IP As String) As Boolean
        Dim blnMatch As Boolean
        blnMatch = System.Text.RegularExpressions.Regex.IsMatch(IP, _
          "^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\." & _
          "(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\." & _
          "(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\." & _
        Return blnMatch
    End Function

    Private Sub btnVerifyIP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVerifyIP.Click

    End Sub

End Class
Question by:simplyamazing
    LVL 11

    Expert Comment

    Just provide a Close button, and set the CausesValidation property to THAT button to false.

    LVL 11

    Expert Comment

    Just provide a Close button, and set the CausesValidation property on THAT button to false.


    Author Comment

    Is there any way to change the CausesValidation property on the winform close button?
    I was hoping to avoid creating a button to close the window as most people will try to close the form using the "X" button at the top right corner, but I can't find any way to access the forms button properties.

    Author Comment

    I tried the button idea and set the causesvalidation property to false,
    then called the form close method


    that did not work, either.

    LVL 11

    Accepted Solution

       Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
            e.Cancel = False
        End Sub

    Evidently, the Closing routine is entered with Cancel preset to true if validation has failed, and setting it to false is the way to override the validiation!


    Author Comment

       Private Sub frmTest3_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
            e.Cancel = False
        End Sub

    slightly different, but it works!   Thanks!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now