I am a newbie. I am using validating procedures to validate user input from 4 text boxes. I am experiencing an issue using the 'x' button to close my form if invalid or no data is present in the textbox with focus when I click the 'x' button. Validating triggers and the message box appears. You can change the data to something valid and close fine but this is aggrevating. I changed the forms causevalidation attribute to false and created a sub to handle formclosing. In that sub I specified that causevalidation for all textboxes be disabled and also set e cancel to false. After that I tested with bad data and the message box showed again, but when I closed it the form closed also. This is better than before but I would like the message box to not show at all. Any suggestions?

6 Years
Discussion Span
Last Post by jlking007

Is this enough or do you need more? It's a homework assignment which is not due yet so I am reluctant to post my code in its entirety. I could PM it to you if needed. Everything works great minus closing with any method other than the close button I created. Everything I try to fix it fails.

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        'Disable validating when form is closed without exit button.
        txtMedication.CausesValidation = False
        txtSurgical.CausesValidation = False
        txtLabFees.CausesValidation = False
        txtRehab.CausesValidation = False
        e.Cancel = False

    End Sub

Edited by jlking007: n/a


I have found the following c# code here and converted it to vb.net.

Public Class Form1

    Const WMCLOSE As String = "WmClose"

    Public Function IsFormClosing() As Boolean
        Dim stackTrace As System.Diagnostics.StackTrace = New System.Diagnostics.StackTrace
        For Each sf As System.Diagnostics.StackFrame In stackTrace.GetFrames
            If (sf.GetMethod.Name = WMCLOSE) Then
                Return True
            End If
        Return False
    End Function

    Private Sub TextBox1_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Validated
        If IsFormClosing() = True Then Exit Sub '// skip the remaining code.
        '// do your validating here.
    End Sub
End Class

Let me try this out and see if it works. Won't get to it till after the Bengals games though! WHO-DEY!!!!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.