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

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

Test the run-time behavior of a UserControl

  1. In Visual Studio, create a Windows control library project, and name it TestContainerExample.

  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.

  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.

Test user controls from another project

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

  1. In Visual Studio, create a Windows control library project, and name it TestContainerExample2.

  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