Here's one way to do it: The idea is
to store a copy of the form fields & values immediately after opening, and
then add a function to detect when the fields have changed. When the user
clicks your close button, call this function and if the values have changed,
add code to submit the form; if they haven't changed, add code to save the form
instead.
Here's some code to get you started.
Everything in black is automatically generated by InfoPath if you make the
right selections. To generate the Loading function, select Developer -> Load
Event. To generate the close button function, right click on your button, then
select Button Properties and then Edit Form Code.
Once you've done that, just add the
code in red. There might be other code and comments in-between the methods
below that is automatically generated by InfoPath, just keep that in mind as
you’re making your changes.
namespace
MyTemplateName
{
public partial class FormCode
{
// add a string to copy over the form
fields & values immediately after loading
string mainXml;
}
public void FormEvents_Loading(object sender, LoadingEventArgs e)
{
// store the fields & values from the main data source into
our mainXml variable
mainXml =
this.MainDataSource.CreateNavigator().SelectSingleNode("/*").InnerXml;
}
// add a method to tell when any of
the form fields have changed
public bool
HasFormChanged()
{
string
currentXml =
this.MainDataSource.CreateNavigator().SelectSingleNode("/*").InnerXml;
bool
returnVal = !currentXml.Equals(mainXml, StringComparison.Ordinal);
return
returnVal;
}
public void MyCloseButtonName(object
sender, ClickedEventArgs e)
{
if (HasFormChanged())
{
// add code to submit the form, because it has changed
}
else
{
// add code to save the form
instead
}
//
add code to close the form
}
}