Debugging Windows Forms Controls.

Creating a control, either a UserControl or one derived by hand from Control or ScrollableControl is a great way of making reusable components that can be recycled in many applications. One of the big problems however is debugging and testing that control.

A common mistake made by many first-time control creators is to add a project to the solution which is used for testing the control. If you don't need to debug any design-time aspects of the control then this can be all you need but there are a few problems associated with this method that makes doing debugging the right way a good idea.

When a test project is hosting a control, a running instance of that DLL is used on the form. In this case, recompilation of the control can sometimes fail because the DLL containing it cannot be overwritten by the compiler as it's in use by the test form. Even closing this form is sometimes not good enough to release the DLL from the IDE's grip and often the only recourse is to close down the IDE and reopen it before a successful compile can be made.

The correct way to test a control is to change the debugging options from "project" which is the default to "program" and have the IDE run another copy of itself in which the debugging can take place. Here's how to do it... 

#1 Select the project properties.

#2 Change the debugging options to Debug Mode: Program and click the button to browse to the program to launch. Note that you'll need to press the "Apply" button once when you change the debug mode.

#2 Change the debugging options to Start external Program and click the button to browse for the program to launch.

#3 Browse to the DEVENV.EXE program. If you installed Visual Studio in the default place this should be somewhere like...

"C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\IDE\devenv.exe"

Now, whenever you hit F5 to run the program, a new instance of Visual Studio will open and you can create the test project that will run your user control in the design-time. You will add your user control to the toolbox in the normal way.

ADVANCED TIP

After you've created the test program you can make sure that your user control always runs the right test code by doing the following actions.

#1 in the IDE in which you created the test program click on the file-open button

#2 right click the solution file and select "Create Shortcut"

#3 Right click the shortcut and click "Properties". When the shortcut properties are displayed, the path to the solution will be highlighted. Press the Ctrl+Ins or Ctrl+C key combination to copy the path to the clipboard.

#4 Return to the IDE in which you designed the control. Select the solution properties again

#5 Paste the path copied from the shortcut into the Command Line Arguments property of the debugging properties.

Now, whenever you hit the F5 key or select Debug->Run the correct test harness will be loaded for your user control.

Return to windows Forms Tips and Tricks

Copyright Ramuseco Limited 2004. All Rights Reserved