Once you've created an ActiveX control, you'll want to debug it. By default, Visual Basic debugs ActiveX controls by displaying them in Internet Explorer when you select Start from the Run menu, as shown in Figure 6-2. Though this lets you see what the control looks like at runtime, it isn't a very good way to debug a control since you can't easily test the control's properties and methods.
Figure 6-2. If you create an ActiveX control project and run it, Visual Basic will display the control in Internet Explorer.
To thoroughly test an ActiveX control, create a project group that contains a test project and the ActiveX control project, following these steps:
The following code tests the Blinker control named blnkTest on a form that also contains a text box named txtStuff:
Option Explicit
Private Sub Form_Load()
`Set the object to blink
blnkTest.Blink txtStuff, 1
End Sub
Private Sub Form_Click()
`Stop the blinker
blnkTest.Interval = 0
End Sub
Private Sub blnkTest_Blinked()
Static intCount As Integer
intCount = intCount + 1
Caption = "Blinked " & intCount & " times"
End Sub
Figure 6-3 shows an example of the Blinker control being tested.
Figure 6-3. Testing the Blinker control.
While debugging a control in-process, you can trace execution from the test project into the ActiveX control's code. You can test the code in your ActiveX control by stepping through execution, setting breakpoints, and using watches, as shown in Figure 6-4.
Figure 6-4. The Blinker control being debugged in-process.
Some of the code in the control runs before you run your test project. To see this, set a breakpoint in the UserControl_Resize event procedure before you draw the control. Visual Basic will jump to the breakpoint when you release the mouse button after drawing the control.
You can modify the code in your ActiveX control at any time, but if you open the UserControl window, the control is disabled on your test form and in the Toolbox. Close the UserControl window to reenable the control.
If you draw a control on your test form and then add design-time properties to the control, Visual Basic disables the control on the form. Terminating code running in the control has the same effect. You can reactivate the control by running the test project.
NOTE
When the ActiveX control is initialized at runtime, properties from other controls on the form might or might not be available to code in the ActiveX control. Referring to a control that was drawn on the form before the ActiveX control was drawn may cause your application to stop responding. This appears to be a bug in Visual Basic.