none
How do I add FormClosing event?

    Question

  •  

    I don't have a File --> Exit menu or anything, just the 'x' in the top right corner.  When I try to manually add a FormClosing event, it gets removed whenever I modify the GUI.  Where can I add it so that it won't go away?

    Monday, August 06, 2007 4:54 PM

Answers

  • That is very strange behavior.
    You can try adding event handler from design like this: Open form in design view, open properties window or press F4, click event toolbar button to view events on Form object, find FormClosing event in Behavior group, and double click it.
    Also try the same thing on new form and also on new form in empty project.
    If you still have this problem then that is surelly problem of your VS installation.

    Monday, August 06, 2007 5:32 PM

All replies

  • It's common thing to lose event handlers connection with events, if you cut and paste controls on the form instead of moving them, but to lose form event handler connection, I didn't see yet. I'm also very confused with your 'manually' word, and have no idea what you mean by that. Can you explain the procedure of attaching handler method to FormClosing event and what you want to do on that event.

    • Proposed as answer by dushkin Thursday, February 16, 2012 2:03 PM
    • Unproposed as answer by dushkin Thursday, February 16, 2012 2:03 PM
    Monday, August 06, 2007 5:03 PM
  • I'm trying to modify InitializeComponent().  Here's what I'm adding:

     

    this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(HMI_FormClosing);

     

    I've created the HMI_FormClosing method already.  This works fine, until I change things on the GUI with the Visual Studio Designer.  Then the above line of code disappears.  How can I prevent that from happening?

    Monday, August 06, 2007 5:09 PM
  • That is very strange behavior.
    You can try adding event handler from design like this: Open form in design view, open properties window or press F4, click event toolbar button to view events on Form object, find FormClosing event in Behavior group, and double click it.
    Also try the same thing on new form and also on new form in empty project.
    If you still have this problem then that is surelly problem of your VS installation.

    Monday, August 06, 2007 5:32 PM
  • Aha!

     

    I was using

    private void HMI_FormClosing(object sender, EventArgs e)

     

    rather than

    private void HMI_FormClosing(object sender, FormClosingEventArgs e)

     

    Thank you very much for your help!! Smile

    Monday, August 06, 2007 6:01 PM
  • Hey Sam,

    If I remember correctly, when you change the GUI the software rebuilds the InitializeComponents, and because your added code was not a part of the GUI it doesn't get written back into the InitializeComponents.

    You might have to use AddHandler and RemoveHandler and create the events at runtime.

    Hope this helps.
    Tuesday, July 22, 2008 6:12 PM