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.ICon tainer
'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.Error Provider
Friend WithEvents ToolTip1 As System.Windows.Forms.ToolT ip
Friend WithEvents txtIPAddress As System.Windows.Forms.TextB ox
Friend WithEvents btnVerifyIP As System.Windows.Forms.Butto n
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Cont ainer
Me.txtIPAddress = New System.Windows.Forms.TextB ox
Me.ErrorProvider1 = New System.Windows.Forms.Error Provider
Me.btnVerifyIP = New System.Windows.Forms.Butto n
Me.ToolTip1 = New System.Windows.Forms.ToolT ip(Me.comp onents)
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.Containe rControl = 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.btnVeri fyIP)
Me.Controls.Add(Me.txtIPAd dress)
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(txt IPAddress, "enter a zip code here")
End Sub
Private Sub txtIPAddress_Validating(By Val sender As Object, ByVal e As System.ComponentModel.Canc elEventArg s) Handles txtIPAddress.Validating
If Not IsIPAddress(txtIPAddress.T ext) Then
e.Cancel = True
txtIPAddress.Select(0, txtIPAddress.Text.Length)
ErrorProvider1.SetError(tx tIPAddress , "Invalid IP address")
End If
End Sub
Private Sub txtIPAddress_Validated(ByV al sender As Object, ByVal e As System.EventArgs) Handles txtIPAddress.Validated
ErrorProvider1.SetError(tx tIPAddress , "")
End Sub
Private Function IsIPAddress(ByVal IP As String) As Boolean
Dim blnMatch As Boolean
blnMatch = System.Text.RegularExpress ions.Regex .IsMatch(I P, _
"^(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
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.ICon
'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.Error
Friend WithEvents ToolTip1 As System.Windows.Forms.ToolT
Friend WithEvents txtIPAddress As System.Windows.Forms.TextB
Friend WithEvents btnVerifyIP As System.Windows.Forms.Butto
<System.Diagnostics.Debugg
Me.components = New System.ComponentModel.Cont
Me.txtIPAddress = New System.Windows.Forms.TextB
Me.ErrorProvider1 = New System.Windows.Forms.Error
Me.btnVerifyIP = New System.Windows.Forms.Butto
Me.ToolTip1 = New System.Windows.Forms.ToolT
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.Containe
'
'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.btnVeri
Me.Controls.Add(Me.txtIPAd
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(txt
End Sub
Private Sub txtIPAddress_Validating(By
If Not IsIPAddress(txtIPAddress.T
e.Cancel = True
txtIPAddress.Select(0, txtIPAddress.Text.Length)
ErrorProvider1.SetError(tx
End If
End Sub
Private Sub txtIPAddress_Validated(ByV
ErrorProvider1.SetError(tx
End Sub
Private Function IsIPAddress(ByVal IP As String) As Boolean
Dim blnMatch As Boolean
blnMatch = System.Text.RegularExpress
"^(25[0-5]|2[0-4][0-9]|[0-
"(25[0-5]|2[0-4][0-9]|[0-1
"(25[0-5]|2[0-4][0-9]|[0-1
"(25[0-5]|2[0-4][0-9]|[0-1
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
Just provide a Close button, and set the CausesValidation property on THAT button to false.
David
David
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 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.
ASKER
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.
then called the form close method
me.close
that did not work, either.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Private Sub frmTest3_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.Canc elEventArg s) Handles MyBase.Closing
e.Cancel = False
End Sub
slightly different, but it works! Thanks!
e.Cancel = False
End Sub
slightly different, but it works! Thanks!
David