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 Chad. He helps ISVs and users in our newsgroup and in this case, it seems that addressing this issue in my blog will be helpful. So to start with, this problem happens if you show a modeless form, but you want to ask the user if they want to save the changes to the form, before the form is closed. If you handle the Closing event, you can do this when the user closes your form. But if the user tries to close the SBA shell directly, your form will close automatically because you will not get the Closing event. Ok, so I hope you understand the problem now.

 

To solve this, we will use the IWin32Window owner parameter that you get from the DoAction callback:

 

Visual Basic

Sub DoAction( _
        ByVal ID As String, _
        ByVal entity As IBaseMasterEntity, _
        ByVal instance As ISmallBusinessInstance, _
        ByVal formsFactory As Object, _
        ByVal owner As IWin32Window _
)

C#

void DoAction(
        string ID,
        IBaseMasterEntity entity,
        ISmallBusinessInstance instance,
        object formsFactory,
        IWin32Window owner
);

 

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:

 

private void 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:

 

protected override void Dispose( bool disposing )

{

    if( disposing )

    {

        if(components != null)

        {

            components.Dispose();

        }

 

        if (this.sbaForm != null)

        {

            this.sbaForm.Closing -= new CancelEventHandler(sbaForm_Closing);

        }

    }

    base.Dispose( disposing );

}