Why is Me.components Nothing?

丶灬走出姿态 提交于 2019-12-25 01:49:52

问题


I have written a custom ErrorProvider which adds some functionality to the existing ErrorProvider (sets control BackColor, ErrorCount etc). This was working find but now for some reason it falls over on the constructor:

_LoginErrorProvider = New ErrorLogErrorProvider(Me.components)

The error is a NullReferenceException which is caused by the fact that Me.components is Nothing. Can anyone shed any light on why a form's components collection would be Nothing? The form seems to work fine in every other way!


回答1:


when you add a component to the design surface it adds this in the InitializeComponent function

me.components = new System.ComponentModel.Container()

so just add this in your self.

or your

_LoginErrorProvider = New ErrorLogErrorProvider(Me.components)

is being called before InitializeComponent




回答2:


You can also drop your ErrorLogErrorProvider class onto the design surface for your Form / UserControl and the code generated for InitializeComponent will correctly initialize the components member and pass it to the constructor of your error provider (VS does this for all non-visual components). Just make sure that your ErrorLogErrorProvider class derives from either Component or implements the IComponent interface.




回答3:


Solved it, adding another component to the form seems to fix the problem, it's a bit of a cludge but works. I suppose the ideal solution would be to add my ErrorProvider to me.components but in order to do this you need to initialize a new instance which you can't because Me.components is Nothing!!

It could drive a man crazy.....



来源:https://stackoverflow.com/questions/516776/why-is-me-components-nothing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!