Link to home
Start Free TrialLog in
Avatar of simplyamazing
simplyamazing

asked on

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

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()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        '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
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    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.SuspendLayout()
        '
        'txtIPAddress
        '
        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 = ""
        '
        'ErrorProvider1
        '
        Me.ErrorProvider1.ContainerControl = Me
        '
        'btnVerifyIP
        '
        Me.btnVerifyIP.Location = New System.Drawing.Point(72, 112)
        Me.btnVerifyIP.Name = "btnVerifyIP"
        Me.btnVerifyIP.TabIndex = 1
        Me.btnVerifyIP.Text = "Verify IP"
        '
        'frmTest3
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.btnVerifyIP)
        Me.Controls.Add(Me.txtIPAddress)
        Me.Name = "frmTest3"
        Me.Text = "frmTest3"
        Me.ResumeLayout(False)

    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)\." & _
          "(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$")
        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
Avatar of anyoneis
anyoneis
Flag of United States of America image

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

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

David
Avatar of simplyamazing
simplyamazing

ASKER

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.
I tried the button idea and set the causesvalidation property to false,
then called the form close method

me.close

that did not work, either.


ASKER CERTIFIED SOLUTION
Avatar of anyoneis
anyoneis
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
   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!