I'm developing a C#
app
And i need do some validations before the user close the form.
I tried to use the FormClosing
event, but it didn't work,
later I used the FormClosed
event, but the same.
The problem is, when I click in the "close button" (on the top of the form) it doesn't do anything but i have the events in form properties and everything.
this is my code:
private void Inicio_FormClosing_1(object sender, FormClosingEventArgs e)
{
//things I have to do
//...
//...
if(bandera==true)
Application.Exit();
}
and
private void Inicio_FormClosed_1(object sender, FormClosingEventArgs e)
{
//things I have to do
//...
//...
if(bandera==true)
Application.Exit();
}
any idea?
Thank you
Both events should work fine. Just open a new project and do this simple test:
private void Form1_Load(object sender, EventArgs e)
{
this.FormClosing += new FormClosingEventHandler(Inicio_FormClosing_1);
this.FormClosed += new FormClosedEventHandler(Inicio_FormClosed_1);
}
private void Inicio_FormClosing_1(object sender, FormClosingEventArgs e)
{
//Things while closing
}
private void Inicio_FormClosed_1(object sender, FormClosedEventArgs e)
{
//Things when closed
}
If you set break points in these methods, you would see that they are reached after the close button is clicked. It seems that there is some problem in your event-attaching code. For example: Inicio_FormClosed_1(object sender, FormClosingEventArgs e)
is wrong, as far as it should take a FormClosedEventArgs
argument; and thus this method is surely not associated with the FormClosed event
(otherwise, the code wouldn't compile).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With