I set up a fairly simple mechanism, as follows:
- After binding my controls, I run a method that finds all the bound
controls and saves their current values (I do a ReadValue() just to
be sure I've got the values from the DataSource) in a Dictionary
that maps a control to its value (there's a small method that gets
the appropriate value for each kind of control that I have).
- I also add a change-event handler for each one (again, the specific
event is determined by the type of control, but they all point to
the same handler)
- The change-handler checks the current value against the Dictionary
value. If it's different then it acts accordingly (in my case it
switches the Close button for the Cancel button). If it's the same
it checks all the other bound controls, so that if nothing is
different it can switch Cancel back to Close; it's a nice feature of
this method that it also recognizes when a change has been undone,
even if it's by re-entering the original value.
- Before leaving, if there are changes to be saved I loop through the
bound controls again to do WriteValue(), just in case WinForms didn't
get around to propagating some change.
I can share the source if anyone is interested.