FormClosing Event


This event occurs as a form is being closed.
The behaviour of this event is different depending on whether you have a modal or modeless windows form.
You can cancel this event to prevent the form closing by setting the Cancel property to True


FormClosing Event

Run code if user exits using the X


Private m_sCurrentValues As String 
Private m_bCancelButtonPressed As Boolean
Private m_bFormLoadComplete As Boolean

Private Sub WindowsForm_FormClosing(ByVal sender As Object, _
                        ByVal e As System.Windows.Forms.FormClosing.EventArgs) _
                        Handles Me.FormClosing

Dim objresult As System.Windows.Forms.DialogResult
Dim slatestvalues As String

   Call Tracer_AddSubroutineStart(System.Reflection.MethodBase.GetCurrentMethod)

   If My.Settings.ERROR_OCCURRED = True Then
'Just exit, don't save any changes
      Exit Sub
   End If

   slatestvalues = Me.GetValues_Latest
   If (m_sCurrentValues = sLatestValues) Or (m_bFormLoadComplete = False) Then

      Call Form_Close()
      Me.Dispose()
      Exit Sub

   Else

      If (m_bCancelButtonPressed = True) Then
         If (gSettingsFile.AreYouSureYouWantToCancel = True) Then
            objresult = modQuestions.WindowsForm_ClosingQuestionCancelConfirmation(m_sFORMTITLE)
         Else
            objresult = Windows.Forms.DialogResult.Yes
         End If
          
         If (objresult = Windows.Forms.DialogResult.Yes) Then
            Call Form_Close()
            Me.Dispose()
            Exit Sub
         End If
         If (objresult = Windows.Forms.DialogResult.No) Then
            e.Cancel = True
            Exit Sub
         End If

      Else 'cross in the corner has been used
         objresult = modQuestions.WindowsForm_ClosingQuestionSaveChangesPrompt(m_sFORMTITLE)

         If (objresult = Windows.Forms.DialogResult.Cancel) Then
            e.Cancel = True
            Exit Sub
         End If
         If (objresult = Windows.Forms.DialogResult.Yes) Then
            If (Form_Save() = False) Then
               e.Cancel = True
               Exit Sub
            End If
            Call Form_Close()
            Me.Dispose()
            Exit Sub
         End If
         If (objresult = Windows.Forms.DialogResult.No) Then
            Call Form_Close()
            Me.Dispose()
            Exit Sub
         End If
      End If
   End If

End Sub

Private Sub btnCancel_Click(ByVal sender As System.Object, _ 
                       ByVal e As System.EventArgs) _
                       Handles btnCancel.Click

   Call Tracer_AddSubroutineStart(System.Reflection.MethodBase.GetCurrentMethod)

   m_bCancelButtonPressed = True
   Me.Close()

End Sub


If (e.CloseReason = CloseReason.UserClosing) Then 
End If


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext