在代码中初始化新标签 - 崩溃

发布于 2024-12-18 06:21:18 字数 454 浏览 2 评论 0原文

我正在将应用程序从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

音盲 2024-12-25 06:21:18

首先,您必须实例化Label 数组。您可以使用 ReDim()New 关键字:

 Private Sub Form_Load() Handles Me.Load
    ReDim lblDataZone(0)   'Or lblDataZone = New Label(0) {}
    lblDataZone(0) = New Label
    With lblDataZone(0)
            .Height = 13
            .Text = "Zone (l/min)"
            .Left = 6
            .Top = 42
    End With
    ....

First of all you have to instantiate the array of Label. You can use ReDim() or New keyword:

 Private Sub Form_Load() Handles Me.Load
    ReDim lblDataZone(0)   'Or lblDataZone = New Label(0) {}
    lblDataZone(0) = New Label
    With lblDataZone(0)
            .Height = 13
            .Text = "Zone (l/min)"
            .Left = 6
            .Top = 42
    End With
    ....
别低头,皇冠会掉 2024-12-25 06:21:18

更改第一个 PUBLIC 以分配所需维度的数组,而不仅仅是声明它:

Public lblDataZone(99) 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

Change the first PUBLIC to allocate the array with the dimension needed, not just declare it:

Public lblDataZone(99) 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文