Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FormClosing and FormClosed events do not work

Tags:

c#

forms

events

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.

enter image description hereenter image description here

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

like image 754
Jean Avatar asked Oct 28 '13 17:10

Jean


1 Answers

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).

like image 129
varocarbas Avatar answered Oct 16 '22 06:10

varocarbas