I would use QueryClose event on Form.
I see 3 ways how solve your task:
- Save data into User Profile (see more details here: GetProfileDocument)
- Save data into notes.ini (more details here: SetEnvironmentString)
- Add a validation on QueryClose that will ask User if it's OK to close Dialog dialog. Parameter Continue controls if Dialog can be closed or not.
See screen with QueryClose event below.

NOTE: if you want to have different behavior on if user clicked Cancel button and Esc than make Cancel as a custom button, so it first set some values to dialog and than fire closing, in such way on QueryClose event you will know how user fired close dialog event.