When a UI Add-in form is open and SBA 2006/Office Accounting 2007 is closed, the UI Add-in form closes without firing the Closed or Closing events. This prevents the developer from asking if the user would like to save their changes before the form closes. SBA forms, if the SBA shell is closed, ask the user if they'd like to save changes before exiting and ISV's would like this behavior as well.
Ok, so I found about this thanks to
To solve this, we will use the IWin32Window owner parameter that you get from the DoAction callback:
Visual Basic |
Sub DoAction( _ |
C# |
void DoAction( |
The IWin32Window is the handle to the window where the menu or toolbar button was pressed, so you can do the following:
this.sbaForm = owner as Form;
if (this.sbaForm != null)
{
this.sbaForm.Closing += new CancelEventHandler(sbaForm_Closing);
}
Then, you can handle the closing event of the SBA Form and prompt the user to save. Since it is a CancelEventHandler you can also cancel the event so the application will not close. Here is a quick example:
privatevoid sbaForm_Closing(object sender, CancelEventArgs e)
{
if (MessageBox.Show("Do you want to close?","Customer Invoices",
MessageBoxButtons.YesNo) == DialogResult.No)
{
e.Cancel = true;
this.BringToFront();
}
else
{
this.Close();
}
}
This is just a quick sample where I simply ask the user to confirm they want to hide my form and if they say no, then I set the Cancel property to true so the form doesn’t close. This content is provided as-is, implies no warranties and confers no rights.
There is a quick catch, if you add this handler for the Closing event, don’t forget to unsubscribe from it when your form is disposed:
protectedoverridevoid Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
if (this.sbaForm != null)
{
this.sbaForm.Closing -= new CancelEventHandler(sbaForm_Closing);
}
}
base.Dispose( disposing );
}