Good Way to Debug Visual Studio Designer Errors
Asked Answered
B

8

43

Is there a good way to debug errors in the Visual Studio Designer?

In our project we have tons of UserControls and many complex forms. For the complex ones, the Designer often throws various exceptions which doesn't help much, and I was wondering if there's some nice way to figure out what has gone wrong.

The language is C#, and we're using Visual Studio 2005.

Burglarious answered 2/9, 2008 at 14:22 Comment(0)
T
7

See Debugging Design-Time Controls (MSDN).

Trinia answered 4/12, 2008 at 7:25 Comment(1)
Great thanks fung! It was especially the "Starting Our Debugging Session" section that helped.Burglarious
S
43

I've been able to debug some control designer issues by running a second instance of VS, then from your first VS instance do a "Debug -> Attach to Process" and pick "devenv".

The first VS instance is where you'll set your breakpoints. Use the second instance to load up the designer to cause the "designer" code to run.

Subsistent answered 2/9, 2008 at 15:22 Comment(5)
What a simple solution! Love it, helped me out greatly.Urian
Why use a second instance to cause the designer code to run? I dont understand why you dont just comment out the ` <System.Diagnostics.DebuggerStepThrough()> ` attribute on the InitializeComponent() method?Bruell
@Jeremy Thompson because Visual Studio is what's hosting and executing any designer-related code (code which doesn't execute when you run your app by itself). If anything you might be able to get by by commenting out any "IsDesignMode" checks. DebuggerStepThrough isn't really the same thing.Subsistent
I see your actually talking about VS Debugging design-timeBruell
top class solution. kudosHodosh
T
7

See Debugging Design-Time Controls (MSDN).

Trinia answered 4/12, 2008 at 7:25 Comment(1)
Great thanks fung! It was especially the "Starting Our Debugging Session" section that helped.Burglarious
L
2

It has been a pain in 2005 and still is in 2015. Breakpoints will often not hit, probably because of the assemblies being shadow copied or something by the designer(?). The best you can do is to break manually by introducing a call to Debugger.Break(). You may wrap it into a compiler conditional as so:

#if DEBUG
   System.Diagnostics.Debugger.Break(); 
#endif
int line_to = break; // <- if a simple breakpoint here does not suffice
Leticia answered 13/7, 2016 at 10:18 Comment(2)
But, is it better in 2016? ;^)Autosome
But is it better in 2017? Of coarse not... Still a bug and annoying as ever and no easy way to resolve it. More than one Microsoft resource actually said it is "by design" that these errors are happening, and that is the most annoying thing of all, that Microsoft refuses to acknowledge it as their problem.Acetify
J
1

I have had this happen many times and it is a real pain.

Firstly I'd suggest attempting to follow the stack trace provided by the designer, though I found that often simply lists a bunch of internals stuff that isn't much use.

If that doesn't work then try compiling and determining the exception from there. You really are flying blind which is the problem. You could then try simply running the code and seeing what exception is raised when you run it, that should give you some more information.

A last-gasp approach could be to remove all the non-generated code from the form and gradually re-introduce it to determine the error.

If you're using custom controls you could manually remove the generated code related to the custom controls as well if the previous method still results in an error. You could then re-introduce this step-by-step in the same way to determine which custom control is causing the problem, then go and debug that separately.

Basically as far as I can tell there's no real way around the problem other than to slog it out a bit!

Jobber answered 2/9, 2008 at 14:36 Comment(0)
C
1

I discovered why sometimes breakpoints are not hit. In the Attach to Process dialog, "Attach to:" type has to be "Select..."'d.

Once I changed to "Managed 4.0, 4.5", breakpoints for a WinRT application were hit. Source: Designer Debugging in WinRT.

Carlettacarley answered 28/4, 2014 at 23:49 Comment(0)
E
0

Each one is different and they can sometimes be obscure. As a first step, I would do the following:

  • Use source control and save often. When a designer error occurs, get a list of all changes to the affected controls that have occurred recently and test each one until you find the culprit
  • Be sure to check out the initialization routines of the controls involved. Very often these errors will occur because of some error or bad dependency that is called through the default constructor for a control (an error that may only manifest itself in VS)
Eldwin answered 2/9, 2008 at 14:32 Comment(0)
I
0

You can run a second instance of VS and attach it to the first instance of VS (Ctrl+Alt+P). In the first instance set the breakpoints, in the second instance run the designer, and the breakpoint will fire. You can step through the code, but Edit-and-Continue will not work.

For Edit-and-Continue to work, set you control library's debug options to run a VS with the command line argument being the solution filename. Then you can simply set the breakpoints and hit F5. It will debug just like user code! As a side note, you can do this will VS and Office add-ins also.

Irresolvable answered 16/5, 2010 at 18:57 Comment(1)
"in the second instance run the designer, and the breakpoint will fire"...does not fire...Orle
B
0

This worked for me for Visual Studio 2022:

  • I opened a second Visual Studio instance
  • In the second instance I clicked Debug -> Attach to Process...
  • I selected DesignToolsServer from the process list

More details: https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/walkthrough-debugging-custom-windows-forms-controls-at-design-time?view=netframeworkdesktop-4.8

Barrault answered 4/5, 2022 at 11:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.