c# - Stop a parent form from closing -


i have winforms application , need way handle following..

basically, have main application form, , within that, have child forms.

the user can hit 'close' on parent form. within child form, things might happening. instance, may have edited databound fields.

i catch close within child, , correctly save changes.

however, want option cancel close. child form prompt user, , cancel application closing.

i tried e.cancel within child form closing event, isn't working- i'm assuming because parent still closing... there way cancel parent form's closing process within child...?

i suggest subscribing formclosing event in main form , validating states each child form , prevent form close (if required). below code might , give fair idea of details.

private void main_formclosing( object sender, formclosingeventargs e )  {     foreach(var f in childforms)     {         if(!f.canclose())         {             e.cancel = true;             return;         }     }      e.cancel = false;    } 

Comments