That dialog box appears because there have been changes. There is a flag on the XDocument object that keeps track of whether there has been any changes. This is the IsDirty flag.
If you are closing your form using code, you can set this flag equal to false, which will prevent the dailog box from appearing. But if the user presses Alt+F4 or clicks the X on the titlebar to close the form, you will cannot intervene to set the flag and prevent that message.

Signature
Greg Collins [InfoPath MVP]
Please visit: http://www.InfoPathDev.com
Does anyone know how to stop the "Do you want to save the changes to form1"
dialog box popping up when you close an infopath form??