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.
Frage
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.
Lösung
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.
Andere Tipps
Consider using Application.Exit()
instead of End
. This allows FormClosing
to be called no matter what (and you can handle it based on how it is being closed).
Private Sub frmMain_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
Select Case e.CloseReason
Case CloseReason.ApplicationExitCall
' This is the result of Application.Exit()
e.Cancel = False
Case CloseReason.UserClosing
' This is the result of clicking the red X
Select Case MessageBox.Show("Are you sure you wish to exit?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
Case DialogResult.Yes
e.Cancel = False
Case DialogResult.No
e.Cancel = True
End Select
Case Else
e.Cancel = False
End Select
If Not e.Cancel Then
VarsToIni()
End If
End Sub
I am quite confused by your question, nonetheless:
This will close all your forms
For each x as Form in My.Application.OpenForms
'You can then put your VarsToIni() here
x.Close()
Next
Note: Add this to your import
Imports System.Windows.Forms