I can't create a WebMessageDialog 2.0

I need to create a WebMessageDialog (Web 2.0) but I can’t get it to display. Where is the Showmodal method in Web 2.0?.

This is the code I use:

Var dialog As New WebMessageDialog
dialog.Message = “¿Estás seguro de qué quieres crear un nuevo experimento?”
dialog.ActionButton.Caption = “Aceptar”
dialog.CancelButton.Visible = True
dialog.CancelButton.Caption = “Cancelar”

Var dialogButton As WebMessageDialogButton
** dialogButton = dialog.Show //No ShowModal or ShowModalWithin option

Select Case dialogButton
Case dialog.ActionButton //Aceptar

MethodOK()

Case dialog.CancelButton //Cancelar

Self.Close

End Select

The line with a ** is the one that gives me an error.

Could anyone help me, please?

WebMessageDialog does not return the button pressed as a result of a Show action. You need to implement the ButtonPressed event.

Use the dialog’s Dismissed event to get whatever action was taken in the dialog.

WebMessageDialog has no Dismissed event. You need to implement ButtonPressed.

The easiest way to do this is to place a WebMessageDialog instance on your WebPage or WebContainer, say with the name dialog, then use this code to show the message:

dialog.Message = "¿Estás seguro de qué quieres crear un nuevo experimento?"
dialog.ActionButton.Caption = "Aceptar"
dialog.CancelButton.Visible = True
dialog.CancelButton.Caption = "Cancelar"
dialog.Show

Then, add the Event Handler ButtonPressed to dialog with the following code:

Select Case button
Case me.ActionButton //Aceptar
MethodOK()
Case me.CancelButton //Cancelar
// We don't need to do anything here as the dialog is already closed.
End Select

It’s important to remember that Web is asynchronous, so you have to think asynchronously. If the Xojo app stopped to wait for a return value the way Desktop apps do, that could create all sorts of problems like locking up the application or failing to communicate with other Sessions.

3 Likes

Hi Anthony,

Thank you very much for your recommendation. I find it the easiest option but I can’t get it to work. I get the error message: This item does not exist

Here is an example: link

I’m sure it’s silly but you can check it out please.

Thank you so much.
Sergio

There were curly quotes in my code block above (from where I copied your code from the first post). I fixed those in the example project – and my previous post – and it works. Your last line in the Button action was also:

dialog.dialogshow.Show

And it should be:

dialog.show

Thank you very much Anthony. It’s true, it works perfectly with your example code.

Great!! :wink:

Sergio

1 Like

Happy to help!

1 Like

My bad. I was thinking of WebDialog for the Dismissed event.

1 Like

Don’t worry Dean. I appreciate your help anyway. :grinning:

Sergio

1 Like

I wrote a generic Method that works for both Desktop and Web. You pass the parameters you want and, on Desktop, it returns a String immediately showing the button pressed. In Desktop you just run the Method and use the returning String. For Web, it requires a few lines of setup and runs via Call, runs different Methods within a Delegate in Session, depending on the Button pressed. Normally I just use the Session.myDelegateActionButton and ignore the Session.myDelegateAlternateActionButton and Session.myDelegateCancelButton.

This is what I copy into many locations within my WebApp. You need to substitute the Action Method XXXXXX and any question description.

Session.myWebMessageDialog = New WebMessageDialog
Session.myDelegateActionButton = AddressOf Self.XXXXXX
Session.myDelegateAlternateActionButton = Nil
Session.myDelegateCancelButton = Nil
AddHandler Session.myWebMessageDialog.ButtonPressed, WeakAddressOf Session.WebMessageDialogButtonPressed
Call CommonWindow.getMessageDialog(Session.myWebMessageDialog, Self, "Are you sure you want to XXXXXX?")

Here is the generic Method it runs:

Protected Function getMessageDialog(myWebMessageDialog As Object, ShowModalWithinWindow As Object, Message As String, ActionButtonCaption As String = "OK", Explanation As String = "", Title As String = "", MessageDialogIcon As String = "Caution", isCancelButtonVisible As Boolean = True, isCancelButtonDefault As Boolean = True, isAlternateActionButtonVisible As Boolean = False, AlternateActionButtonCaption As String = "") as String
  #If TargetDesktop Or TargetWeb Then 'not for Console
    #If TargetDesktop Then
      Var tempMessageDialog As New MessageDialog                  // declare the MessageDialog object
      Var tempMessageDialogButton As MessageDialogButton                // for handling the result
    #ElseIf TargetWeb Then
      'Var tempMessageDialog As New WebMessageDialog                 // declare the MessageDialog object
      Var tempMessageDialog As WebMessageDialog = WebMessageDialog(myWebMessageDialog)                 // declare the MessageDialog object
      Var tempMessageDialogButton As WebMessageDialogButton                // for handling the result
    #EndIf
    
    tempMessageDialog.Message = Message
    tempMessageDialog.ActionButton.Caption = ActionButtonCaption
    #If TargetDesktop Then
      Select Case MessageDialogIcon
      Case "Caution"
        tempMessageDialog.IconType = MessageDialog.IconTypes.Caution
      Case "Note"
        tempMessageDialog.IconType = MessageDialog.IconTypes.Note
      Case "Question"
        tempMessageDialog.IconType = MessageDialog.IconTypes.Question
      Case "Stop"
        tempMessageDialog.IconType = MessageDialog.IconTypes.Stop
      Case Else
        tempMessageDialog.IconType = MessageDialog.IconTypes.Caution
      End Select
    #EndIf
    
    tempMessageDialog.Explanation = Explanation 'on macOS smaller text below Message, on others it is same size.
    tempMessageDialog.Title = Title
    tempMessageDialog.CancelButton.Visible = isCancelButtonVisible
    If isCancelButtonVisible Then
      If isCancelButtonDefault Then
        tempMessageDialog.CancelButton.Default = True
        tempMessageDialog.ActionButton.Default = False 'we can't have two default buttons!
      Else
        tempMessageDialog.ActionButton.Default = True 'Default?
        tempMessageDialog.CancelButton.Default = False
      End If
    End If
    tempMessageDialog.AlternateActionButton.Visible = isAlternateActionButtonVisible
    tempMessageDialog.AlternateActionButton.Caption = AlternateActionButtonCaption
    
    #If TargetDesktop Then
      If ShowModalWithinWindow = Nil Then 'check if we need to link it to a specific window
        tempMessageDialogButton = tempMessageDialog.ShowModal 'display the dialog
        
      ElseIf ShowModalWithinWindow IsA Window Then 'ensure the object is a Window!
        Var tempWindow As Window = Window(ShowModalWithinWindow)
        If tempWindow = Nil Then
          tempMessageDialogButton = tempMessageDialog.ShowModal 'display the dialog
        Else
          tempMessageDialogButton = tempMessageDialog.ShowModalWithin(tempWindow) 'display the dialog within the provided Window
        End If
        
      Else
        tempMessageDialogButton = tempMessageDialog.ShowModal 'display the dialog
      End If
      
      Select Case tempMessageDialogButton 'determine which button was pressed.
      Case tempMessageDialog.ActionButton
        Return "Action"
      Case tempMessageDialog.AlternateActionButton
        Return "AlternateAction"
      Case tempMessageDialog.CancelButton
        Return "Cancel"
      Case Else
        Return "Cancel"
      End Select
      
    #ElseIf TargetWeb Then
      tempMessageDialog.Show 'display the dialog, the button clicked is already set as Delegates
    #EndIf
  #EndIf
  
End Function
2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.