2015年4月15日 星期三

[VB]使用者控制項屬性可以用陣列

本處實做一個燈號使用者控制項,狀態數量由屬性"狀態數量"設定,
,不同狀態燈號圖片由上圖狀態圖示插入。

CODE:

Public Class PilotLight

    Dim stateNum As UInteger = 2  '指示燈狀態數量
    Dim statePIC As Image() = New Image(8) {} '該狀態圖示(狀態0開始)
    Dim pic_sizemode As PictureBoxSizeMode = PictureBoxSizeMode.StretchImage '圖效果
    Dim uniqeID As String               '物件獨特ID
    Dim state As UInteger = 0             '目前燈狀態
  ............................................

    Sub New()

        '使指示燈圖拉長或縮小以符合大小
        InitializeComponent()
        PictureBox1.SizeMode = pic_sizemode
        PictureBox1.Image = statePIC(state)
        ' Add any initialization after the InitializeComponent() call.

    End Sub
    Property 狀態數量 As UInteger  '最多9組 最少2組
        Get
            Return stateNum
        End Get
        Set(value As UInteger)
            If value > 10 Then
                stateNum = 9
            ElseIf value < 2 Then
                stateNum = 2
            Else
                stateNum = value
            End If
        End Set
    End Property
    Property 狀態圖示 As Image()
        Get
            Return statePIC
        End Get
        Set(value As Image())
            statePIC = value
        End Set
    End Property

.............................下略

[VB問題]並未將物件參考設定為物件的執行個體



原錯誤CODE:

Public Class PilotLight

    Dim stateNum As UInteger = 2  '指示燈狀態數量
    Dim statePIC As Image()           '該狀態圖示(狀態0開始)
        ==>改Dim statePIC As Image() = New Image(9) {}
    Dim pic_sizemode As PictureBoxSizeMode = PictureBoxSizeMode.StretchImage '圖效果
    Dim uniqeID As String               '物件獨特ID
    Dim state As UInteger = 0             '目前燈狀態
.....................

    Sub New()

        '使指示燈圖拉長或縮小以符合大小
        InitializeComponent()
        PictureBox1.SizeMode = pic_sizemode
        ' PictureBox1.Image = statePIC(state)
        ' Add any initialization after the InitializeComponent() call.

    End Sub
    Property 狀態數量 As UInteger  '最多9組 最少2組
        Get
            Return stateNum
        End Get
        Set(value As UInteger)
            If value > 10 Then
                stateNum = 9
            ElseIf value < 2 Then
                stateNum = 2
            Else
                stateNum = value
            End If
        End Set
    End Property
    Property 狀態圖示0 As Image
        Get
            Return statePIC(0)
        End Get
        Set(value As Image)
            statePIC(0) = value
        End Set
    End Property
    Property 狀態圖示1 As Image
        Get
            Return statePIC(1)
        End Get
        Set(value As Image)
            statePIC(1) = value
        End Set
    End Property
    Property 狀態圖示2 As Image
        Get
            Return statePIC(2)
        End Get
        Set(value As Image)
            statePIC(2) = value
        End Set
    End Property
    Property 狀態圖示3 As Image
        Get
            Return statePIC(3)
        End Get
        Set(value As Image)
            statePIC(3) = value
        End Set
    End Property
    Property 狀態圖示4 As Image
        Get
            Return statePIC(4)
        End Get
        Set(value As Image)
            statePIC(4) = value
        End Set
    End Property
    Property 狀態圖示5 As Image
        Get
            Return statePIC(5)
        End Get
        Set(value As Image)
            statePIC(5) = value
        End Set
    End Property
    Property 狀態圖示6 As Image
        Get
            Return statePIC(6)
        End Get
        Set(value As Image)
            statePIC(6) = value
        End Set
    End Property
    Property 狀態圖示7 As Image
        Get
            Return statePIC(7)
        End Get
        Set(value As Image)
            statePIC(7) = value
        End Set
    End Property
    Property 狀態圖示8 As Image
        Get
            Return statePIC(8)
        End Get
        Set(value As Image)
            statePIC(8) = value
        End Set
    End Property
................................................................



簡化寫法參考使用者控制項屬性可以用陣列!