B4J Code Snippet Save changes before closing with a resumable sub

B4X:
Sub MainForm_CloseRequest (EventData As Event)
   EventData.Consume
   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
       MainForm.Close
   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:
Top