Saturday, February 14, 2009

VS.NET Designer Errors

Unknown-Designer-Exceptions Maintaining old applications has always been something painful for me. Old application, for me, means Winform applications using DataSets for binding operations. When converting and opening an old application, the least thing you want on your hands is to open a form and see Designer screen of death. This is due to the fact that in the WinForm world when you open a form, all the extra codes in the constructor and Load events will get executed, and this where things can go hairdo. Think about a pieced of code in constructor of a form doing something innocent in nature like saving / loading data from registry, connecting to a service. Almost always VS.NET shows a cryptic message. How would you know which code / form / usercontrol is causing this?

Debugging the Designer

There are just a few steps to catch the buggy piece of code ruining your day. Open up your visual studio solution containing your application’s code and compile. Locate the form that is breaking the designer. Open another instance of VS.NET and Goto “Tools” –> "Attach To Process” and locate the process named “Devend.exe” which is Visual Studio’s process name. There are two processes named “Devenv”. Make sure you select the one with loaded solution. To catch all the thrown exceptions go to “Debug” –> “Exceptions” and check the column “Thrown” in front of “Common Language Runtime Exceptions”. This will tell the VS.NET to catch all the exception thrown by CLR. At this point you’re good to go. Switch to your VS.NET containing the solution and open the Form / Usercontrol. The exception will be caught so you can have a very good idea what the problem is.

VSDebugger

It is very easy to avoid this.

  • Make sure the codes written in Form and Usercontrol constructors / OnLoaded / Loaded event (except for InitializeComponents in constructors) will not get executed.
  • Use the DesignMode property

If you’re extending functionalities of a custom control you need to be aware of this problem. The constrcutors of custom controls will be called in InitializeComponent anyway, so if there’s something wrong with the code in there, you’ll end up with this problem.

There's also an issue with the DesignMode property in UserControls. The design mode only works when the user control is Sited so you may be in design mode and get a False value when checking DesignMode property. The trick is to use a property on System.ComponentModel.LicenseManager which is more reliable.


Submit this story to DotNetKicks Shout it

No comments: