Error "Value cannot be null. Parameter name: objectType" when opening a VB.NET Windows Forms in designer

I have Windows Forms App in VB.NET it runs fine, I added some code event manually, without designer, like this:

Private Sub bindingNavigatorAddNewItem_Click(sender As System.Object, e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click

namesBindingSource.AddNew()

End Sub

And one the Form1.Designer.vb, InitializeComponent method this code

AddHandler Me.BindingNavigatorAddNewItem.Click, AddressOf Me.bindingNavigatorAddNewItem_Click

Now, even though the form runs fine, and the event is executed correctly; the form fails at design time with the 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.CodeDom.CodeMemberMethod.get_Statements() 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 System.ComponentModel.Design.Serialization.BasicDesignerLoader.BeginLoad(IDesignerLoaderHost host)

Now, I notice it doesn't happen if I bind the event by designer, instead of manually.

But I still need to do it manually, because it will eventually be generated by some DTE automation code.

Also if after the error I press the link "Ignore and Continue" it shows the form fine at design time, but why it is failing in the first place?

Upvotes: 2

Views: 16035

Answers (3)

Found it, the code for the event needs to have the "Handles..." clause removed, like

Private Sub bindingNavigatorAddNewItem_Click(sender As System.Object, e As System.EventArgs)

namesBindingSource.AddNew()

End Sub

Now the Form opens correctly in the Designer right from the first time.

UPDATE: I had to instead add the handler with AddHandler statement.

Upvotes: 0

StevieTimes
StevieTimes

Reputation: 465

I found another cause of this as well...

Check the upper/lower case on your event names! One of our controls got renamed, so it was CtlBlah instead of ctlBlah. The event was still in lower case. Switching it to proper case to match the actual control fixed it.

Upvotes: 1

StevieTimes
StevieTimes

Reputation: 465

Here is how you fix this:

  1. Go into the form code
  2. Delete everything in between Public Class (your form name) and End Class.

Open the form designer. Did it open normally?

If yes, you're on the right track.

Put the code back; little by little, keep removing code until your form opens. The last code you deleted was the problem.

This happens when an event handler contains a reference to a non-existing object. For example, if you place a button on a form, create the Click event handler and then remove this button from the form.

Upvotes: 8

Related Questions