Problem solve Get help with specific problems with your technologies, process and projects.

Debugging design-time functionality

Debugging design-time components can be a little tricky.

A component is a nonvisual class designed specifically to integrate with a design-time environment such as Visual Studio .NET. WinForms provides several standard components, and .NET lets you build your own, gaining a great deal of design-time integration with very little work.

Because a component is a class that's made to be integrated into a design-time host, it has a life separate from the run-time mode that we normally think of for objects. It's not enough for a component to do a good job when interacting with a user at run time as per developer instructions; a component also needs to do a good job when interacting with the developer at design time.

This short excerpt from InformIT explains how to set up design-time debugging of your components.

To demonstrate the .NET Framework's various design-time features and services, I've built a sample. Because components and controls share the same design-time features and because I like things that look snazzy, I built a digital/analog clock control with the following public members (click here for an example of this clock):

public class ClockControl : Control {
  public ClockControl();
  public DateTime Alarm { get; set; }
  public bool IsItTimeForABreak { get; set; }
  public event AlarmHandler AlarmSounded;

When you build design-time features into your components, you'll need to test them and, more than likely, debug them. To test run-time functionality, you simply set a breakpoint in your component's code and run a test application, relying on Visual Studio .NET to break at the right moment.

What makes testing design-time debugging different is that you need a design-time host to debug against; an ordinary application won't do. Because the hands-down hosting favorite is Visual Studio .NET itself, this means that you'll use one instance of Visual Studio .NET to debug another instance of Visual Studio .NET with a running instance of the component loaded. This may sound confusing, but it's remarkably easy to set up:

  1. Open the component solution to debug in one instance of Visual Studio .NET.
  2. Set a second instance of Visual Studio .NET as your debug application by going to Project | Properties | Configuration Properties | Debugging and setting the following properties:
    1. Set Debug Mode to Program.
    2. Set Start Application to <your devenv.exe path>\devenv.exe.
    3. Set Command Line Arguments to <your test solution path>\yourTestSolution.sln.
  3. Choose Set As StartUp Project on your component project.
  4. Set a breakpoint in the component.
  5. Use Debug | Start (F5) to begin debugging.

At this point, a second instance of Visual Studio.NET starts up with another solution, allowing you to break and debug at will.

The key to making this setup work is to have one solution loaded in one instance of VS.NET that starts another instance of VS.NET with a completely different solution to test your component in design mode.

To read more about building design-time components click over to InformIT.

Dig Deeper on .NET Framework security best practices

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.