Android Code Snippet Sharing the goodness: MsgBox Things

I'm using the code snippets below for my message boxes. I have these running in a module.

First I define the vb like constants, I kept on forgetting with the DialogResponse constants so I decided on this, seeing coming from a vb background..

B4X:
Public vbYes As Int = -1
    Public vbNo As Int = -2
    Public vbCancel As Int = -3
    Public UseIcons As Boolean = True

Then I defined a sub to return the constants based on what a user selects in the normal msgbox.

B4X:
'return msgbox results
Sub vbYesNoCancel(ans As Int) As Int
    Select Case ans
    Case DialogResponse.POSITIVE
        Return vbYes
      Case DialogResponse.CANCEL
          Return vbCancel
    Case DialogResponse.NEGATIVE
          Return vbNo
    End Select
End Sub

Then I defined the various methods for my message boxes, with these you pass the title and the message prompt to display and they will return the vblike msgbox constants for YesNoCancel and YesNo prompts.

B4X:
' return an error prompt
Sub Error(sTitle As String, sMessage As String)
    If UseIcons = True Then
        Msgbox2(sMessage, sTitle & " Error", "Ok", "", "", LoadBitmap(File.DirAssets, "exclaim.png"))
    Else
        Msgbox2(sMessage, sTitle & " Error", "Ok", "", "", Null)
    End If
End Sub


' show an alert
Sub Alert(sTitle As String, sMessage As String)
    If UseIcons = True Then
        Msgbox2(sMessage, sTitle, "Ok", "", "", LoadBitmap(File.DirAssets, "exclaim.png"))
    Else
        Msgbox2(sMessage, sTitle, "Ok", "", "", Null)
    End If
End Sub

' show yesnocancel
Sub YesNoCancel(sTitle As String, sMessage As String) As Int
    Dim answ As Int
    If UseIcons = True Then
        answ = Msgbox2(sMessage, sTitle, "Yes", "Cancel", "No", LoadBitmap(File.DirAssets, "question.png"))
    Else
        answ = Msgbox2(sMessage, sTitle, "Yes", "Cancel", "No", Null)
    End If
    Return vbYesNoCancel(answ)
End Sub



' show yesno
Sub Confirm(sTitle As String, sMessage As String) As Int
    Dim answ As Int
    If UseIcons = True Then
        answ = Msgbox2(sMessage, sTitle, "Yes", "", "No", LoadBitmap(File.DirAssets, "question.png"))
    Else
        answ = Msgbox2(sMessage, sTitle, "Yes", "", "No", Null)
    End If
    Return vbYesNoCancel(answ)
End Sub

' show yesno
Sub YesNo(sTitle As String, sMessage As String) As Int
    Return Confirm(sTitle,sMessage)
End Sub

' inform
Sub Inform(sTitle As String, sMessage As String)
    If UseIcons = True Then
        Msgbox2(sMessage, sTitle, "Ok", "", "", LoadBitmap(File.DirAssets, "info25.png"))
    Else
        Msgbox2(sMessage, sTitle, "Ok", "", "", Null)
    End If
End Sub

Off course, you can use your own icons.
 

Attachments

  • exclaim.png
    exclaim.png
    981 bytes · Views: 550
  • info25.png
    info25.png
    789 bytes · Views: 441
  • question.png
    question.png
    1.1 KB · Views: 464
Top