Android Question [Solved] Using MsgBox2Async inside a "HandleAction" Sub

Gavin

Member
Licensed User
Longtime User
Hello everyone,
Firstly, the following code

B4X:
Sub SKB_HandleAction As Boolean
    'We use the HandleAction event,
    'so as to prevent the Keyboard
    'closing, if the user forgets
    'to enter a File Name.
    Dim e As EditText
    e.Initialize("")
    e = Sender
    
    etFile.Text = etFile.Text.Trim
    If etFile.Text = "" Then
        Dim Result As Int = Msgbox2("Please Enter a File Name.", "No File Name Entered.", "", "Cancel", "Ok", Null)
        If Result = DialogResponse.NEGATIVE Then
            Return True
        Else If Result = DialogResponse.CANCEL Then
            etFile.Text = lblTitle.Text
            CallSub(Me, "lblSave_Click")
            Return False
        End If
    End If   
End Sub

works perfectly.

If the user does not enter a File Name, the message box appears, warning the user of his / her mistake, waits for the reply, then either dismisses the Message Box and waits for a file name of returns the current file name and continues on.

The problem is, I want to do the right thing and replace the Magbox2 with MsgBox2Async.

Now, I have successfully used MsgBox2Async elsewhere in the program but, none of the Subs where MsgBox2Async reside need to return a value.
SKB_HandleAction on the other hand Does need to return a value to it's calling sub, to inform as to weather or not to hide the Keyboard.
As Erel informs in his Video on Resumable Subs, executing Sleep or Wait For is akin to Returning so, SKB_HandleAction immediately wants to return to it's calling sub, whereas a Modal Message Box will wait until it is dismissed.
So, how do I use MsgBox2Async correctly inside SKB_HandleAction.
SKB_HandleAction needs to wait for the Message Box to be dismissed until returning to its calling sub.

MC73 did post an example of calling a different sub where MsgBox2Async resided but, again, the calling sub did not need to return a value.

Thank You
Gavin
 

agraham

Expert
Licensed User
Longtime User
SKB_HandleAction needs to wait for the Message Box to be dismissed until returning to its calling sub.
It can't., it has to return. You make the calling Sub (and the one calling that and the one calling ...) a Resumable Sub and wait on the completion of SKB_HandleAction.
 
Upvote 0

Gavin

Member
Licensed User
Longtime User
Thankyou agraham and Spavlyuk

It appears that the solution was a lot easier than I thought.
I modified the SKB_HandleAction as below and added the sub OpenMsgBox, as per mc73's suggestion in another post.

B4X:
Sub SKB_HandleAction As Boolean
    'We use the HandleAction event,
    'so as to prevent the Keyboard
    'closing, if the user forgets
    'to enter a File Name.
    Dim e As EditText
    e.Initialize("")
    e = Sender
   
    etFile.Text = etFile.Text.Trim
    If etFile.Text = "" Then
        OpenMsgBox
        Return True    'Return True to keep the Keyboard Open. The Message Box will remain until we respond.
    Else
        lblTitle.Text = etFile.Text 'If we have a valid "File Name",
        CallSub(Me, "lblSave_Click")'Save it,
        Return False                'hide the Keyboard and get out of here.
    End If
   
End Sub

Sub OpenMsgBox
   
    Msgbox2Async("Please Enter a File Name.", "No File Name Entered.", "", "Cancel", "Ok", Null, False)
    Wait For Msgbox_Result(Result As Int)
    If Result = DialogResponse.CANCEL Then    'No need to process the NEGATIVE Response, only the CANCEL Response.
        etFile.Text = lblTitle.Text            'If we cancel, load the edittext with the current file and
        ime.HideKeyboard                    'get out of here.
    End If                                    'Too easy.

End Sub

So far, so good.

Thank you
Gavin
 
Upvote 0
Top