It’s a fact that the Visual Studio 2010 Designer has some problems when dealing with inherited windows forms with a very very deep inheritance hierarchy (inheritance level > 2).
Working on one of my legacy project I converted from VS2008 to VS2010, I had to edit some forms and when I tried to open them in the designer I got the following error:
Value cannot be null. Parameter name: objectType
at System.ComponentModel.TypeDescriptor.TypeDescriptionNode.GetRuntimeType(Type objectType)
at System.ComponentModel.TypeDescriptionProvider.GetRuntimeType(Type reflectionType)
at Microsoft.VisualStudio.Design.MultiTargetingContextProvider.GetRuntimeType(Type objectType)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.HandlesClauseManager.GetFieldType(String fieldName, Type documentBaseType)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.HandlesClauseManager.GetReferencedComponentType(String componentName, CodeTypeDeclaration codeTypeDecl, ITypeResolutionService loader, IDictionary& cache)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.HandlesClauseManager.ParseHandlesClauses(CodeTypeDeclaration codeTypeDecl, Boolean updateCache)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomParser.OnMethodPopulateStatements(Object sender, EventArgs e)
at System.ComponentModel.Design.Serialization.TypeCodeDomSerializer.Deserialize(IDesignerSerializationManager manager, CodeTypeDeclaration declaration)
at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.DeferredLoadHandler.Microsoft.VisualStudio.TextManager.Interop.IVsTextBufferDataEvents.OnLoadCompleted(Int32 fReload)
My first reaction was: Panic! sometimes the problem was there and sometimes (very few times to be honest) I could open the forms and edit them correctly.
The designer offer us the choice to ignore the error and go on editing...using that I had two possible results:
- The form opens up correctly in the designer without altering the code.
- A complete environment crash.
Having these crashes is not acceptable and time loosing, I had to find the cause of this problem. So I gave an in-depth look at what Visual Studio is telling me with this error: walking the stack trace you can find that the HandlesClauseManager class is actually the source of all problems. The name of the class and the fact that the project was written in Visual Basic rang a bell, so I started to comment out all the ‘handles eventname’ clauses on every WithEvents variable on the target form, save and reopen the form in the designer designer and everything seemed to work properly.
Once you find the cause of the problem, the next step is to reproduce the issue in a new empty test project; so far I’ve been able to reproduce this problem in two different ways:
- Adding a control to the form, attaching some handlers with the designer and then remove the control. if some of the function event handlers remain on the code with the ‘handles eventname’ there you have your error, because the Designer is trying to attach the events to a non existing object. A build of the project will however spot the problem.
- Using inherited forms: I declared a protected WithEvents variable on the base form:
Public Class Form1 Protected WithEvents bmb As CurrencyManager Private Sub bmb_CurrentChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles bmb.ListChanged ' ... End Sub End Class
This form works correctly and opens up in the designer. Since we had a very deep hierarchy I inherited a second form and attached the event to the CurrencyManager in this form too:
Public Class Form2 ' taken from the partial class code file for illustrative purposes Inherits WindowsApplication1.Form1 Private Sub bmb_CurrentChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles bmb.ListChanged End Sub End Class
This forms opens up correctly in the designer, but the interesting thing is that if you switch to the code editing view and try to open up the combobox showing all the form members the CurrencyManager is not listed there (it was on Visual Studio 2008), that’s not a big issue I just attached the event directly coding it, nonetheless this is a warning that inherited forms will have problems with events attached to protected WithEvents variables in some inherited forms.
As I said before our form hierarchy inheritance tree is quite deep, so I added a third form inherited from Form2 and again I attached the event manually:
Public Class Form3 Inherits WindowsApplication1.Form2 Public Sub bmb_CurrentChanged2(ByVal sender As Object, ByVal e As System.EventArgs) Handles bmb.CurrentChanged End Sub End Class
Trying to open up this form in the designer finally shows the problem. it will not work, and sometimes ignoring the issue and forcing the designer to open the whole Visual Studio crashes.
The workaround to the problem here is simple: attach the events using ‘addhandler’ and remove them using ‘removehandler’ rather than having the HandlesClauseManager do the dirty work for you. C# is naturally immune to this problem.
- NUnitit: Visual Studio Addin to support NUnit and some customizations to it (26/08/2015)
- Resharper and the OutOfMemoryException problem (26/08/2015)
- Spot Hidden Exceptions using IntelliTrace (26/08/2015)
- VS2008 SP1 + PowerCommands = Toolbox AddItems...Crash! (26/08/2015)
- How to update your TFS Workspace after CodePlex upgrade to TFS2010 (26/08/2015)
- More related document (12)