在代码中初始化新标签 - 崩溃
我正在将应用程序从 VB6 转换为 VB.NET,并且需要在代码中声明和初始化一些控件(控件数组)。我将其声明为全局,以便其他表单可以访问存储在其中的数据,然后我尝试在 Form_Load() 子组件中操作它们:
Public lblDataZone() As Label
Private Sub Form_Load() Handles Me.Load
lblDataZone(0) = New Label
With lblDataZone(0)
.Height = 13
.Text = "Zone (l/min)"
.Left = 6
.Top = 42
End With
我得到的错误是“对象引用未设置为对象的实例”。我觉得我在这里错过了一些巨大的东西,但这有什么问题吗?
谢谢
I'm converting an application from VB6 to VB.NET, and am required to declare and initialise some controls in code (control arrays). I have it declared globally, so other forms can access the data stored within them, and then I try to manipulate them inside the Form_Load() sub:
Public lblDataZone() As Label
Private Sub Form_Load() Handles Me.Load
lblDataZone(0) = New Label
With lblDataZone(0)
.Height = 13
.Text = "Zone (l/min)"
.Left = 6
.Top = 42
End With
The error I get says "Object reference not set to an instance of an object". I feel like I'm missing something huge here, but what's wrong with it?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您必须实例化
Label
数组。您可以使用ReDim()
或New
关键字:First of all you have to instantiate the array of
Label
. You can useReDim()
orNew
keyword:更改第一个 PUBLIC 以分配所需维度的数组,而不仅仅是声明它:
Change the first PUBLIC to allocate the array with the dimension needed, not just declare it: