views:

44

answers:

1

I've got a control that crashes on the form designer when I build the application, and am trying to figure out how to debug the problem.

I thought all I needed to do to be able to get in with a debugger was to start a second copy of VS and use Debug-Attach to process and attach to the copy of visual studio that the solution with my troublesome control is in. I did that but nothing is happening when the control crashes so I know I'm doing something wrong...

The crash occurs in the designer and returns the messagebox:

---------------------------
Microsoft Visual Studio
---------------------------
The control NameSpace.MyControl has thrown an unhandled exception in the designer and has been disabled.  



Exception:

Could not load file or assembly 'OtherProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.



Stack trace:

   at NameSpace.MyControl.OnPaint(PaintEventArgs e)
---------------------------
OK   
---------------------------

OtherProject is part of the solution and is referenced by the project with both the form and the custom control in it.

When I dismiss the messagebox the control shows a stacktrace where the control is on my form, but since it doesn't include line numbers I don't know where the problem is coming from.

+2  A: 

The search path for assemblies is different when they get loaded at design time, it will be probing path for Visual Studio. Which is configured by the devenv.exe.config file in Common7\IDE. Only the Public Assemblies and Private Assemblies folders are included. The build directory of your project is not considered.

Modifying this .config file or copying your assembly into one of these folders is not exactly practical. By far the best thing to do is to just not call the code that requires this assembly at design time. Use the DesignMode property:

    protected override void OnPaint(PaintEventArgs e) {
        if (!this.DesignMode) {
            // Runtime painting code here
            //...
        }
        base.OnPaint(e);
    }
Hans Passant
I'm trying to find where the crash is coming from so I can suppress only the offending bit of code and not have completely blank controls in the designer. Part of my problem is that the project was started by someone else and the controls are interacting with each other in ways I'm not expecting as a result. In this case, the crash is occurring in controlA, but only happens if controlB is on the form.
Dan Neely
I obviously can't help you find that for you, not sure what you expected me to say. The DesignMode property is key.
Hans Passant
That's why I was asking about how to attach a debugger. I want to get the line it's crashing on and look at what's been called within the controls to trigger it. The bit of the stack shown in he designer doesn't have line numbers or show anything except the framework methods that lead to the OnPaint call so it's not helping me narrow the problem down.
Dan Neely
The JIT compiler bombs when it can't generate code for the OnPaint method. There is thus nothing to look at. Move the custom drawing code into its own method so it will at least enter the OnPaint method before bombing.
Hans Passant