Simple 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.

The default is to operate without CallBack. In this simple mode, a form will be dismissed (hidden) immediately after you click on a commandbutton or the closebox.

Example

Here is the code for the 'Replicator Service' form :

Option Explicit

Const vbModal = 1
Dim oDlg, oReplicatorForm, sBtn

'Get a reference to the WshDialog.Kit automation object
Set oDlg = Wscript.CreateObject("WshDialog.Kit")

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

'Show the form (modally)
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")
    
'Show the caption of the optionbutton that was chosen
    MsgBox "You choose " & oReplicatorForm.Ctl(sBtn).Caption
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

    
'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"
    oFrm.NewButton "Cancel", 2500, 2700, 750, 375, "&Cancel"
    
'Enable the Cancel property of the Cancel button, so ESC can be used
    oFrm.Ctl("Cancel").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, 2500, 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"

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

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

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

    
'Return the form object
    Set BuildReplicatorForm = oFrm

End Function