Window's form - control what happens when user closes it

I have a windows form. If the user goes to FILE>>CLOSE to close the form, code is executed to save some information. But, how can I get the same code to be executed if the user clicks the X at the top right of the form to close it

Thanks!

~Lauren




Answer this question

Window's form - control what happens when user closes it

  • pbrowntwi

    If that is true, where would that line of code go in my program

    ~Lauren



  • Sean Walker

    hi,

    you can put that code in intializecomponent method, but don't forget to add the handler for this method

    or

    select your form and go to properties

    you will find a lightening icon at top click it , it will show you all the events that you can use with your form

    select closing and double click it, it will creat the handler for that event for you in the code view

    add any code you want to excecute in that handler

    hope this helps



  • Gayathri

    It worked perfectly. Thank you for your help!

    EDIT: the going to lightening bolt part

    ~Lauren



  • Alan Robbins

    i think you can use the form closed event

    FormClosed += new FormClosedEventHandler(MyForm_FormClosed);

  • Window's form - control what happens when user closes it