Udostępnij za pośrednictwem


How to: Test the Run-Time Behavior of a UserControl

When you develop a UserControl, you need to test its run-time behavior. You can create a separate Windows-based application project and place your control on a test form, but this procedure is inconvenient. A faster and easier way is to use the UserControl Test Container provided by Visual Studio. This test container starts directly from your Windows control library project.

Important

For the test container to load your UserControl, the control must have at least one public constructor.

Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Working with Settings.

Note

A Visual C++ control cannot be tested using the UserControl Test Container.

To test the run-time behavior of a UserControl

  1. Create a Windows control library project called TestContainerExample. For details, see Windows Control Library Template.

  2. In the Windows Forms Designer, drag a Label control from the Toolbox onto the control's design surface.

  3. Press F5 to build the project and run the UserControl Test Container. The test container appears with your UserControl in the Preview pane.

  4. Select the BackColor property displayed in the PropertyGrid control to the right of the Preview pane. Change its value to ControlDark. Observe that the control changes to a darker color. Try changing other property values and observe the effect on your control.

  5. Click the Dock Fill User Control check box below the Preview pane. Observe that the control is resized to fill the pane. Resize the test container and observe that the control is resized with the pane.

  6. Close the test container.

  7. Add another user control to the TestContainerExample project. For details, see How to: Add Existing Items to a Project.

  8. In the Windows Forms Designer, drag a Button control from the Toolbox onto the control's design surface.

  9. Press F5 to build the project and run the test container.

  10. Click the Select User Control ComboBox to switch between the two user controls.

Testing User Controls from Another Project

You can test user controls from other projects in your current project's test container.

To test user controls from another project

  1. Create a Windows control library project called TestContainerExample2. For details, see Windows Control Library Template.

  2. In the Windows Forms Designer, drag a RadioButton control from the Toolbox onto the control's design surface.

  3. Press F5 to build the project and run the test container. The test container appears with your UserControl in the Preview pane.

  4. Click the Load button.

  5. In the Open dialog box, navigate to TestContainerExample.dll, which you built in the previous procedure. Select TestContainerExample.dll and click the Open button to load the user controls

  6. Use the Select User Control ComboBox to switch between the two user controls from the TestContainerExample project.

See Also

Tasks

How to: Author Composite Controls

Walkthrough: Authoring a Composite Control with Visual Basic

Walkthrough: Authoring a Composite Control with Visual C#

Reference

UserControl

User Control Designer