簡體   English   中英

Visual Basic:圖片框數組的聲明

[英]Visual Basic: Declaration of Array of Pictureboxes

為什么下面的代碼在運行時給出空引用異常?(假設計時器在窗體打開時開始計時)。 我要在許多子程序中使用該數組,如果可能的話,我不想在每個子程序中聲明該數組以使其工作,因為這會使程序變得很長。

注意:圖片框Enemy1_1,Enemy1_2,Enemy1_3等從一開始就已經在窗體上。

Public Class Form1
    Dim Array1() As PictureBox = {Enemy1_1, Enemy1_2, Enemy1_3}
    Dim Array2() As PictureBox = {Enemy2_1, Enemy2_2, Enemy2_3}
    Dim Array3() As PictureBox = {Enemy3_1, Enemy3_2, Enemy3_3}

    Private Sub timer1_Tick(sender As Object, e As EventArgs) Handles timer1.Tick
        For index As Integer = 0 To 2
            Array1(index).Left += 5
            Array2(index).Left += 5
            Array3(index).Left += 5
        Next
    End Sub
End Class

也許您在尋找比下面更復雜的東西,哈哈...但是至少它可能會阻止頁面轟炸,和/或確認發生異常的地方

Public Class Form1
    Dim Array1() As PictureBox = {Enemy1_1, Enemy1_2, Enemy1_3}
    Dim Array2() As PictureBox = {Enemy2_1, Enemy2_2, Enemy2_3}
    Dim Array3() As PictureBox = {Enemy3_1, Enemy3_2, Enemy3_3}

    Private Sub timer1_Tick(sender As Object, e As EventArgs) Handles timer1.Tick
        For index As Integer = 0 To 2
            If Array1(index) IsNot Nothing Then
                Array1(index).Left += 5
            End If
            If Array2(index) IsNot Nothing Then
                Array2(index).Left += 5
            End If
            If Array3(index) IsNot Nothing Then
                Array3(index).Left += 5
            End If
        Next
    End Sub
End Class

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM