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.