An Example in Visual Basic

This example uses the Clock Scriptlet component we created earlier in the chapter, within a normal Visual Basic application—and proves that our Scriptlets can behave just like ordinary ActiveX controls. In Design view, the VB window looks like this. You can see the Scriptlets control in the Toolbox, and the Clock component on the Form:

The source code for this project is in the Scriptlets/Vbslet folder of the samples that you can download from our Web site at http://rapid.wrox.co.uk/books/0707.

What we need to do is

If the Scriptlet requires code to be run when it loads, we do this in the onReadyStateChange event rather than in the Form_Load event. Until the control's readyState property is equal to 4 (i.e. loading is complete), it's unsafe to execute code within the Scriptlet because is in an inconsistent state.

The next screenshot shows the final application running, and you'll recognize the Clock component:

The application behaves like the Clock HTML page we saw earlier. In particular, it contains a button that sets an alarm in seconds, and changes the background color of the corresponding textbox to emphasize this. We multiply by 1000 since the Clock component's method expects its input in milliseconds:

Scriptlet1.Alarm (Val(Text1.Text) * 1000)
Text1.BackColor = &HC0C0C0

The alarm originates regular events that are handled in onScriptletEvent within the VB code:

Sub Scriptlet1_onscriptletevent(ByVal name As String, ByVal eventData As Variant)
  If name = "OnAlarm" Then
    Scriptlet1.FgColor = "red"
    Beep
    res = MsgBox("Would you stop the alarm?", _
          vbExclamation Or vbYesNo, "Clock Alarm!")
    If res = vbYes Then
       Scriptlet1.Alarm 0
       Text1.BackColor = &HFFFFFF
    End If
    Scriptlet1.FgColor = "lightgreen"
  End If
End Sub

In addition, the application shows the current location of the Scriptlet, and allows you to stop the clock and set a label.