Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 661
  • Last Modified:

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
0
simplyamazing
Asked:
simplyamazing
  • 3
  • 3
1 Solution
 
anyoneisCommented:
Just provide a Close button, and set the CausesValidation property to THAT button to false.

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

David
0
 
simplyamazingAuthor Commented:
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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
simplyamazingAuthor Commented:
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.


0
 
anyoneisCommented:
   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!

David
0
 
simplyamazingAuthor Commented:
   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!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now