C
C#6h ago
Fumetsu

Handle Closing Form?

Hello, I'm having some trouble handling the closing of my form. Currently I'm using the method below, but it for some reason runs 5 times instead of just once.
private bool Closing = false;
Classes Classes = new Classes();

public MainForm()
{
InitializeComponent();
Application.ApplicationExit += new EventHandler(this.Application_ApplicationExit);
}


private void Application_ApplicationExit(object? sender, EventArgs e)
{
Classes.Spark.DebugLog("The app is currently being closed!");
}
private bool Closing = false;
Classes Classes = new Classes();

public MainForm()
{
InitializeComponent();
Application.ApplicationExit += new EventHandler(this.Application_ApplicationExit);
}


private void Application_ApplicationExit(object? sender, EventArgs e)
{
Classes.Spark.DebugLog("The app is currently being closed!");
}
5 Replies
phaseshift
phaseshift6h ago
You must be making 5 mainforms then 😆 (or adding the same event handler somewhere else, or just running the method otherwise) Use a debugger to look
Fumetsu
FumetsuOP6h ago
Actually I think you're correct. I'm using MainForm MainForm = new MainForm() to access my MainForms controls from my other files. Is there another method I could use to access the MainForms methods/contains without creating a new one?
phaseshift
phaseshift6h ago
Yes, pass the mainform as a variable to the constructor of things that need to call methods on it
Fumetsu
FumetsuOP6h ago
Ahhh okay I'll look into that, thanks for all the help!
phaseshift
phaseshift6h ago
👍 gl

Did you find this page helpful?