Search code examples
vb.netwinformsformclosing

What is the best way to detect all ways of closing a form?


I have a form that i close using End several times in my code. I want to do a command to save settings when I do this called VarsToIni() which takes some public variables and saves them in an INI file. I have tried putting it in the main window's FormClosing (which stays open throughout) and this only works when you close from pressing the X button not from my End statement.


Solution

  • Add a new Sub and replace calls to End with calls to your new Sub:

    Sub EndMe()
        VarsToIni()
        Application.Exit() 
    End Sub
    

    Edit:

    As Dan points out, End() is a bad way to close the application, Application.Exit() is preferred.