B4J Question [SOLVED] jXUI messagebox not waiting for result

Lahksman

Active Member
Licensed User
Longtime User
I was just testing the jXUI messagebox and it's behaving strange.

When I call frm_CloseRequest from a cancel button my messagebox is shown and waits for the result.
However when i close the form with the close-button the messagebox pops up and dissapears immediatly and the form closes.

The commented code works as expected in both situations.

The form itself is a modal form.

B4X:
Sub frm_CloseRequest (EventData As Event)
    Dim sf As Object = xui.Msgbox2Async(mResources.getResource(Main.c_language,"messageboxChanges") & CRLF & mResources.getResource(Main.c_language,"messageboxConfirm"), "", mResources.getResource(Main.c_language,"yes"), "", mResources.getResource(Main.c_language,"no"), Null)
    Wait For (sf) Msgbox_Result (Result As Int)
    If Result = xui.DialogResponse_Positive Then
        frm.Close
    End If
   
'    If fx.Msgbox2(frm,mResources.getResource(Main.c_language,"messageboxChanges") & CRLF & mResources.getResource(Main.c_language,"messageboxConfirm"),"",mResources.getResource(Main.c_language,"yes"),"",mResources.getResource(Main.c_language,"no"),fx.MSGBOX_CONFIRMATION) = fx.dialogresponse.POSITIVE Then
'        frm.Close
'    End If
End Sub

B4X:
Sub btnUserCancel_MouseClicked (EventData As MouseEvent)
    frm_CloseRequest(EventData)
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You need to remember one thing about resumable subs:
Wait For and Sleep are equivalent to Return from the caller point of view.

This means that CloseRequest will end before the result is available and as you haven't called EventData.Consume (which is the recommended way to prevent it from closing) then it will be closed.

Correct code:
B4X:
Sub MainForm_CloseRequest (EventData As Event)
   EventData.Consume
   Wait For (xui.Msgbox2Async("Close?", "", "Yes", "Cancel", "No", Null)) Msgbox_Result (Result As Int)
   If Result = xui.DialogResponse_Positive Then
     MainForm.Close
   End If
End Sub
 
Upvote 0
Top