Unlike other programmable objects, controls have both design-time and run-time behavior. That is, some of the code in your control will execute when a developer places an instance of the control on a form at design time.
For example, the code you place in the UserControl_Resize event procedure will be executed both at design time and at run time.
In order to debug the design-time behavior of your control, you must be able to execute code in the control while the test form on which you place the control remains in design mode.
The following two procedures demonstrate this neat trick. In the first procedure, you’ll add code to the Resize event of the ShapeLabel control. In the second procedure, you’ll put part of ControlDemo into run mode — while the test project remains in design mode — and then add an instance of the ShapeLabel control to a form in the test project.
Note This topic is part of a series that walks you through creating a sample ActiveX control. It begins with the topic Creating an ActiveX Control.
To add code to the Resize event
Private Sub UserControl_Resize()
Static intCt As Integer
intCt = intCt + 1
Debug.Print "Resize " & intCt
End Sub
Note The name of the event procedure has the prefix "UserControl," just as the Form_Resize event procedure for an ordinary form has the prefix "Form."
In developing an ordinary Visual Basic application, you would now click the Start button on the toolbar, or press F5, to run your application. In order to put a ShapeLabel control on Form1, however, you have to run just the code for the control, leaving everything else in design mode.
To run the ShapeLabel control at design time
Important Don’t click the Start button on the toolbar, or press F5, because this would put the entire project group into run mode, and you would be unable to add the new control to a form.
When you put a control in run mode, it doesn’t matter how you close the designer’s window. (You always can tell if the designer is open, because the control’s toolbox icon will be grayed.)
In the Properties window you can see the default properties for a new control. The ShapeLabel control you just added to the form has been given a default name, ShapeLabel1.
Note Naming your control when you begin designing it avoids confusion. Suppose you place a control with a default name, such as UserControl1, on a form. Automatic numbering of new controls would append a number to the control name, resulting in a confusing name like UserControl11.
If you simply move the control around the form, the Resize event does not occur.
Opening a control’s designer makes all instances of the control inactive. Changing the code in the control’s code window may also make control instances inactive.
If the control has become inactive because of changes to its code, you can right-click the test form to bring up its context menu, and click Update UserControls to reactivate control instances.
Note Due to the number of windows required by these procedures, you may frequently find that ShapeLabel’s designer has disappeared behind another form. You can double-click ShapeLabel in the Project Explorer window to bring the designer to the front.
For More Information More information about running code at design time can be found in "Debugging Controls," in "Building ActiveX Controls."
This topic is part of a series that walks you through creating a sample ActiveX control.
To | See |
Go to the next step | Life and Times of a UserControl Object |
Start from the beginning | Creating an ActiveX Control |