Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey every body i still don't understand how to know whether the close button is clicked or not in visual studio 2008 and what is its event name and how do i do it.

please i need ur help
Thanks.
Posted

The way I track this is I have a boolean variable on my form. I set it to false in the load event. If I have a button or other control on my form that the user can use to close the form, I make sure that it sets the variable to true before it calls the Form.Close. Then I use the FormClosing event to catch things like checking if the user saved before exiting:

VB
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


Hope this helps
 
Share this answer
 
You can use the FormClosingEventArgs.CloseReason property to check if the Close button was clicked.

VB
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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900