Dear John, How Do I... Debug a Control?

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.

Click to view at full size.

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:

  1. If the ActiveX control project is currently loaded, save it by choosing Save Project from the File menu.

  2. From the File menu, choose New Project to display the New Project dialog box.

  3. Double-click the Standard EXE icon. Visual Basic creates a new project with a single form that you can use to test your ActiveX control.

  4. From the File menu, choose Add Project to display the Add Project dialog box.

  5. Select the ActiveX control project from the Existing tab on the Add Project dialog box, and then click Open.

  6. Close the UserControl window if it is open. Once the window is closed, Visual Basic activates the control's Toolbox icon.

  7. Click the ActiveX control's Toolbox icon, and draw the control on your test project's form.

  8. Write code in the test project to access the control's properties and methods.

  9. Run the test project.

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.

Click to view at full size.

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.