在设计器中打开 VB.NET Windows 窗体时出现错误“值不能为空。参数名称:objectType”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23818003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error "Value cannot be null. Parameter name: objectType" when opening a VB.NET Windows Forms in designer
提问by Fernando Gonzalez Sanchez
I have Windows Forms App in VB.NET it runs fine, I added some code event manually, without designer, like this:
我在 VB.NET 中有 Windows 窗体应用程序它运行良好,我手动添加了一些代码事件,没有设计器,如下所示:
Private Sub bindingNavigatorAddNewItem_Click(sender As System.Object, e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click
namesBindingSource.AddNew()
End Sub
Private Sub bindingNavigatorAddNewItem_Click(sender As System.Object, e As System.EventArgs) 处理 BindingNavigatorAddNewItem.Click
名称BindingSource.AddNew()
结束子
And one the Form1.Designer.vb, InitializeComponent method this code
还有一个Form1.Designer.vb,InitializeComponent方法这段代码
AddHandler Me.BindingNavigatorAddNewItem.Click, AddressOf Me.bindingNavigatorAddNewItem_Click
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)
值不能为空。参数名称:objectType
在 System.ComponentModel.TypeDescriptor.TypeDescriptionNode.GetRuntimeType(Type objectType) 在 System.ComponentModel.TypeDescriptionProvider.GetRuntimeType(TypereflectionType) 在 Microsoft.VisualStudio.Design.MultiTargetingContextProvider.GetRuntimeType(Type objectType) 在 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.ManagerCla .ParseHandlesClauses(CodeTypeDeclaration codeTypeDecl, Boolean updateCache) 在 Microsoft.VisualStudio.Design.Serialization.CodeDom。VSCodeDomParser.OnMethodPopulateStatements(Object sender, EventArgs e) 在 System.CodeDom.CodeMemberMethod.get_Statements() 在 System.ComponentModel.Design.Serialization.TypeCodeDomSerializer.Deserialize(IDesignerSerializationManager manager, CodeTypeDeclaration 声明) 在 System.ComponentModel.Design.Serialization.CodeDom PerformLoad(IDesignerSerializationManager manager) 在 Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager) 在 System.ComponentModel.Design.Serialization.BasicDesignerLoader.BeginLoad(IDesignerLoaderHost 主机)在 System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager) 在 Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager) 在 System.ComponentModel.Design. .BasicDesignerLoader.BeginLoad(IDesignerLoaderHost 主机)在 System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager) 在 Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager) 在 System.ComponentModel.Design. .BasicDesignerLoader.BeginLoad(IDesignerLoaderHost 主机)BasicDesignerLoader.BeginLoad(IDesignerLoaderHost 主机)BasicDesignerLoader.BeginLoad(IDesignerLoaderHost 主机)
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.
但是我还是需要手动去做,因为它最终会被一些DTE自动化代码生成。
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?
此外,如果在错误之后我按下链接“忽略并继续”,它会在设计时很好地显示表单,但为什么它首先失败了?
回答by StevieTimes
Here is how you fix this:
这是您解决此问题的方法:
- Go into the form code
- Delete everything in between Public Class (your form name) and End Class.
- 进入表单代码
- 删除 Public Class(您的表单名称)和 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.
当事件处理程序包含对不存在对象的引用时,就会发生这种情况。例如,如果在窗体上放置一个按钮,请创建 Click 事件处理程序,然后从窗体中删除此按钮。
回答by StevieTimes
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.
检查事件名称的大写/小写!我们的一个控件被重命名,所以它是 CtlBlah 而不是 ctlBlah。该事件仍为小写。将其切换到适当的大小写以匹配实际控件修复它。
回答by Fernando Gonzalez Sanchez
Found it, the code for the event needs to have the "Handles..." clause removed, like
找到了,事件的代码需要去掉“Handles...”子句,比如
Private Sub bindingNavigatorAddNewItem_Click(sender As System.Object, e As System.EventArgs)
namesBindingSource.AddNew()
End Sub
Private Sub bindingNavigatorAddNewItem_Click(sender As System.Object, e As System.EventArgs)
名称BindingSource.AddNew()
结束子
Now the Form opens correctly in the Designer right from the first time.
现在,表单从第一次就可以在设计器中正确打开。
UPDATE: I had to instead add the handler with AddHandler statement.
更新:我不得不使用 AddHandler 语句添加处理程序。

