Visual Basic Concepts

Running the ShapeLabel Control at Design Time

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

  1. In the Project Explorer window, double-click ShapeLabel to make it the active designer.

  2. Double-click the ShapeLabel control to open the code window.

  3. In the Procedure box, click the Resize event to go to its event procedure. Add the following code:

    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

  1. In the Project window, double-click ShapeLabel to bring its designer to the front, then press CTRL+F4 to close the window. Closing the designer’s window puts the ShapeLabel control in run mode. As soon as the control is in run mode, its icon (the default toolbox icon for a user control) is enabled in the toolbox.

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

  2. In the Project Explorer window, double-click Form1 to bring it to the front.

  3. Double-click the ShapeLabel icon to add a ShapeLabel control to Form1. The control appears as a flat gray rectangle with grab handles:

    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.

  4. The ShapeLabel control’s Resize event occurred when it was placed on the form, as you can see by looking at the Immediate window. Use the grab handles to resize the control several times. Each time you resize it, the Resize event occurs again.

    If you simply move the control around the form, the Resize event does not occur.

  5. On Form1, double-click the ShapeLabel control to open the code window for Form1. The cursor will be on the default event procedure, ShapeLabel1_GotFocus. You can use the Procedure box to view the other three events Visual Basic automatically provides for your control. Close the code window when you are done.

  6. In the Project Explorer window, double-click ShapeLabel to open the ShapeLabel designer. Notice that the ShapeLabel control you placed on Form1 is shaded with hatch marks to indicate that it is inactive.

    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.

  7. Code in ShapeLabel’s code module cannot be executed while the designer is open. Use the grab handles to resize the shaded ShapeLabel control on Form1. The Resize event doesn’t fire, so no new messages appear in the Immediate window.

  8. Be sure the ShapeLabel designer is in front, then press CTRL+F4 to close the window, reactivating the control instance. The shading disappears from the control on Form1, indicating that the instance is active again.

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

Step by Step

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