CallBack Forms


Each WshDialog form can operate in one of two ways; with or without event handling (also known as CallBack). You need the CallBack feature if you want to react to events like clicking an optionbutton, checkbox or other control without dismissing the form.

To demonstrate the CallBack feature, I've improved the
Simple Form Replicator Service with an option to add sugar to your tea. This checkbox will appear only when the appropriate optionbutton 'Tea, Earl Grey' is chosen. When a different drink is selected, the checkbox disappears.

Although this is only a minor improvement, it does show the potential of CallBack forms.
You could, for instance, add a timer control to a form to check for the arrival of new mail each time the control raises it's Timer event.

Also see the
ClickHandler topic, which describes the event handler, and the Events Handled topic for a list of all the events that can be handled.

Example

Next, you'll find the code for the 'Improved Replicator Service' form.
( changes to the non-callback version are shown in
blue )

Option Explicit

Const vbModal = 1
Dim oDlg, oReplicatorForm, sBtn
, sMsg

'Get a reference to WshDialog.Kit and set the event handler prefix to oDlg_
Set oDlg = Wscript.CreateObject("WshDialog.Kit"
, "oDlg_")

'Call BuildReplicatorForm to build the form and store a reference in oReplicatorForm
Set oReplicatorForm = BuildReplicatorForm

'Show the Replicator form
oReplicatorForm.Show vbModal

'Check which button was clicked to dismiss (close) the form
Select Case Ucase(oDlg.Clicked)
Case "OK"
    
'Get the name of the optionbutton that was chosen
    sBtn = oReplicatorForm.GetOptionButton("FRM1")
    sMsg = "You choose " & oReplicatorForm.Ctl(sBtn).Caption
    If sBtn = "OBN1" Then
        If oReplicatorForm.Ctl("Sugar").Value = 1 Then
            sMsg = sMsg & " with Sugar"
        Else
            sMsg = sMsg & " without Sugar"
        End If
    End If
    'Show the name of the drink that was selected
    MsgBox
sMsg
Case Else
    
'Either the Cancel button or the CloseBox was used to dismiss the form
    MsgBox "You cancelled the Replicator Service"
End Select

'Let's tidy up (not technically necessary)
oReplicatorForm.Clear
WScript.DisconnectObject oDlg
Set oDlg = Nothing

'-----------------------------------------------------------------------------------------------------------------
' The BuildReplicatorForm function builds the form and returns a reference to it
'-----------------------------------------------------------------------------------------------------------------

Function BuildReplicatorForm

    Dim oFrm
, oCtl

    
'Create the form
    Set oFrm = oDlg.NewForm

    
'Add OK and Cancel buttons (the first button becomes the default button)
    oFrm.NewButton "OK", 1250, 2700, 750, 375, "&OK"
    
Set oCtl = oFrm.NewButton("Cancel", 2750, 2700, 750, 375, "&Cancel")
    
'Enable the Cancel property of the Cancel button, so ESC can be used
    oCtl.Cancel = True

    
'Add a standard QuestionMark icon
    oFrm.NewImage "IMG1", 250, 200 , 0, 0, "IconQuestion"

    
'Add a label
    oFrm.NewLabel "LBL1", 1150, 300, 3000, 375, "What do you want to drink ?"

    
'Add a frame
    oFrm.NewFrame "FRM1", 1000, 700,
2750, 1650, " Replicator Menu "

    
'Add three optionbuttons to the FRM1 frame
    oFrm.NewOptionButton "OBN1", 150, 300, 2000, 375, "Tea, Earl Grey", True, "FRM1"
    oFrm.NewOptionButton "OBN2", 150, 700, 2000, 375, "Cardassian Ale", False, "FRM1"
    oFrm.NewOptionButton "OBN3", 150, 1100, 2000, 375, "Raktajino", False, "FRM1"

    
'Add a Sugar checkbox to the FRM1 frame
    
oFrm.NewCheckBox "Sugar", 1750, 300, 750, 375, "Sugar",, "FRM1"

    'Set the form's caption
    oFrm.Caption = "
Improved Replicator Service"

    
'Show the form in the taskbar
    oFrm.TaskBar = True

    
'Automatically size the form to the controls placed on it
    oFrm.AutoSize

    
'Enable event handling (callback) for this form
    
oFrm.CallBack = True

    
'Return the form object
    Set BuildReplicatorForm = oFrm

End Function

'-----------------------------------------------------------------------------------------------------------------
' oDlg_ClickHandler handles the events sent by the controls
'-----------------------------------------------------------------------------------------------------------------

Sub oDlg_ClickHandler(sForm, sControl)

    Dim oFrm

    
'Get a reference to the form that raised the event
    Set oFrm = oDlg.Frm(sForm)

    
'Since there is only one form we don't have to test the sForm parameter !

    'Check which control caused the event
    Select Case UCase(sControl)
    Case "OBN1"
        'Optionbutton 1 is selected; show the Sugar checkbox
        oFrm.Ctl("Sugar").Visible = True
    Case "OBN2", "OBN3"
        
'Optionbutton 2 or 3 is selected; hide the Sugar checkbox
        oFrm.Ctl("Sugar").Visible = False
    Case "OK", "CANCEL", "*CLOSE"
        'Dismiss the form when a commandbutton or the closebox is clicked
        oFrm.Hide
    Case Else
        'Ignore all other events. Do NOT use oFrm.Hide here, or any event
        'not handled above (like the Sugar checkbox) will dismiss the form

    End Select

End Sub