Android Question How to return a result code from a Class that uses a dialog

Gary Milne

Active Member
Licensed User
Longtime User
I have a Class that generates a color picker dialog with several sliders and buttons (Save, Cancel, Reset)

I want to return an Integer value from calling the SHOW routine.

B4X:
Public Sub Show() As Int

    lblTitle.Text = Title
    pnlColor.Visible = True
  
    Update_Current_Color
  
    Do While True
        'DoEvents
        If ColorSelected = True Then
            Return NewColor
        Else
            WaitFewSeconds(500)
            Update_Current_Color
        End If
    Loop

End Sub

This works but I don't like it. It basically spins until the user presses one of the three buttons which changes the value of ColorSelected from False to True.

I know that I could do a CallBack but that seems more complex than simply returning an Int value.

My gut tells me this is a bad solution but perhaps I'm wrong. I've looked for other examples of providing a return value from a GUI class but have not found one.

Any opinions from someone with a bit more experience would be welcome. I'd class myself as advanced beginner.

After some reading it seems that what I'm looking for is advice on creating a Modal Dialog. Sounds like it should be easy but apparently that is not the case.
 
Last edited:

Gary Milne

Active Member
Licensed User
Longtime User
The way to do it is to use calsub from the class to the activity's matched sub. See example here http://www.b4x.com/android/forum/threads/class-vertical-horizontal-and-circle-seekbars.19444/
Thanks for the pointer. I had never come across the #Event tag before. I will have to experiment with it, that should work quite well I think. Seems a lot like a CallBack but I need to play with it a bit.

I like those seekbars BTW and I think you wrote the Color Picker too which is the best one I've come across. The only thing missing is some indentation in the code :)
 
Upvote 0

derez

Expert
Licensed User
Longtime User
The only thing missing is some indentation in the code
To my understanding the code is indented. Please explain.
Do you mean that this
B4X:
Sub Button1_Click
mycol.GetColor1(12)
End Sub
Should be this:
B4X:
Sub Button1_Click
    mycol.GetColor1(12)
End Sub
?
 
Upvote 0

ivan.tellez

Active Member
Licensed User
Longtime User
B4X:
    Do While True
        'DoEvents
        If ColorSelected = True Then
            Return NewColor
        Else
            WaitFewSeconds(500)
            Update_Current_Color
        End If
    Loop


This code is not really responsive, and will drain the battery of the device if you don't close the "dialog" asap.
 
Upvote 0

Gary Milne

Active Member
Licensed User
Longtime User
To my understanding the code is indented. Please explain.
Do you mean that this
B4X:
Sub Button1_Click
mycol.GetColor1(12)
End Sub
Should be this:
B4X:
Sub Button1_Click
    mycol.GetColor1(12)
End Sub
?
Screen Shot 2014-10-04 at 10.54.03 AM.png

This is what it looks like in my compiler. This is from the Seekbars class. May be a setting on my end but I think mine is pretty vanilla. Just FYI.
 
Upvote 0

Gary Milne

Active Member
Licensed User
Longtime User
Thanks to everyone that responded. It seems like Callbacks are the easiest\best solution when implementing something like this. No more responses needed, thanks.
 
Upvote 0
Top