I need to override the Close button an a C++/CLI Form application, allowing the user to cancel the close.

This must have been asked a zillion times, but I can't find an example specific to C++/CLI Form applications.

My Form class starts with:

namespace TR31Forms {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
    using namespace FXCore;  // fxcore.dll namespace

	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{

I found (at http://msdn.microsoft.com/en-us/library/system.windows.forms.form.onclosing.aspx) the Form:OnClosing Method with the following example:

private:
   void Form1_Closing( Object^ /*sender*/, System::ComponentModel::CancelEventArgs^ e )
   {
      // Determine if text has changed in the textbox by comparing to original text.
      if ( textBox1->Text != strMyOriginalText )
      {
         // Display a MsgBox asking the user to save changes or abort.
         if ( MessageBox::Show( "Do you want to save changes to your text?", "My Application", MessageBoxButtons::YesNo ) == ::DialogResult::Yes )
         {
            // Cancel the Closing event from closing the form.
            e->Cancel = true;

            // Call method to save file...
         }
      }
   }

but the Form1_Closing() method is not called when closing the form, so I am missing something probably obvious.

I probably need to register Form1_Closing(), but I don't know how.

Thanks for any help.

1.) Select the Form1.h [Design] tab in your project
2.) Click once on Form1 itself
3.) Go to the Properties Window (by default on the right side, but if you can't see it go to View/Other Windows/Properties Window
4.) In the properties window go to the little lightning bolt on the toolstrip (under Form1 System.Windows.Forms.Form
5.) Under behavior, double click the empty cell next to Form closing.

Now you'll have the proper event handler added and you can flesh out the method definition as you see fit.

1.) Select the Form1.h [Design] tab in your project
2.) Click once on Form1 itself
3.) Go to the Properties Window (by default on the right side, but if you can't see it go to View/Other Windows/Properties Window
4.) In the properties window go to the little lightning bolt on the toolstrip (under Form1 System.Windows.Forms.Form
5.) Under behavior, double click the empty cell next to Form closing.

Now you'll have the proper event handler added and you can flesh out the method definition as you see fit.

Great, thanks!

This question has already been answered. Start a new discussion instead.