Use the OnCloseQuery event of the main form of your app. Obviously, you have to set the CanClose parameter.