Friday, March 28, 2014

How to Catching the close button event click


This is an example code for knowing when we click on close button:

Example 1:

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        MessageBox.Show("Closing Form")
End Sub

Example 2:

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If boolCloseByButton Then
           'Closed by a different button          
        Else
           'Closed by the x on the form
        End If
End Sub


Example 3:

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    'You could put a Select Case statement here and take action for all
    'different types of closing conditions.    
    If e.CloseReason = System.Windows.Forms.CloseReason.UserClosing Then
          'Closed by the x on the form or Alt-F4
    Else  
          'Closed for some other reason      
             
    End If

End Sub

No comments:

Post a Comment