B4J Code Snippet Save changes before closing with a resumable sub

Sub MainForm_CloseRequest (EventData As Event)
   Dim sf As Object = xui.Msgbox2Async("Save changes?", "Title", "Yes", "Cancel", "No", Null)
   Wait For (sf) Msgbox_Result (Result As Int)
   If Result = xui.DialogResponse_Positive Then
       'save changes
   End If
   If Result <> xui.DialogResponse_Cancel Then
   End If
End Sub
The code might be a bit confusing. Remember that from the calling method perspective, a call to Wait For or Sleep is equivalent to a call to Return.
This means that we must consume the event before calling Wait For. Otherwise the form will be closed.

If the user didn't click on cancel then we call Form.Close to close it. The CloseRequest event will not be fired in that case.
Last edited: