2011-11-28 69 views
2

我正在將應用程序從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 

我得到的錯誤說「對象引用不設置到對象的實例「。我覺得我在這裏錯過了很多東西,但是它有什麼問題?

感謝

回答

4

首先你必須實例的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 
    .... 
+0

輝煌,謝謝大家。 – Nick

1

變更首次公開分配與所需的維數組,而不是僅僅把它聲明:

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