Multiple Forms


WshDialog allows you to build and manage multiple forms simultaneously.

You can, at any time, access the properties and methods of any existing form or control
(as long as you haven't Cleared the form(s) of course).

This ability to maintain multiple forms can, for instance, be used to build a series of installation screens with the usual 'Next' and 'Back' buttons or, as shown in the example below, you could show a splash screen before the actual form.

Example

Here's the code that is needed to add an opening splash screen to the 'Improved Replicator Service' form that was demonstrated in the previous topic on CallBack Forms
( additions to the original code are shown in
blue, removed lines in grey )

Option Explicit

Const vbModal = 1
Dim oDlg,
oSplashForm, 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
'Build both the Splash form and the Replicator form
Set oSplashForm = BuildSplashForm
Set oReplicatorForm = BuildReplicatorForm

'Show the Splash form
oSplashForm.Show vbModal

'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 BuildSplashForm function builds the form and returns a reference to it
'-----------------------------------------------------------------------------------------------------------------

Function BuildSplashForm

    
Dim oFrm, oCtl

    
'Create the Splash form
    
Set oFrm = oDlg.NewForm("Splash")

    
'Set the form's caption
    oFrm.Caption = "WshDialog v1.0.4"

    
'Add an image
    
oFrm.NewImage "IMG", 0, 0, 0, 0, "<somepath> \Splash.bmp"

    
'Add a timer with a 3 second interval
    
oFrm.NewTimer "Timer", 3

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

    
'Disable the closebox and control menu
    
oFrm.CloseBox = False

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

    
'Return the form object
    
Set BuildSplashForm = oFrm

End Function

'-----------------------------------------------------------------------------------------------------------------
' 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 !

    Select Case Ucase(sForm)
    Case "SPLASH"
        'Check which control caused the event
        Select Case UCase(sControl)
        Case "TIMER"
            'Disable the timer, or it would continue to fire events
            oFrm.Ctl("Timer").Enabled = False
            'Dismiss the Splash form
            oFrm.Hide
        Case Else
        End Select
    
Case Else
        '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 Select

End Sub