使用临时变量循环标签数组

时间:2015-11-05 18:07:57

标签: vb.net

我在10x10矩阵中有一个包含100个标签的2D数组(它是2D的,因为它代表了现实世界中的一些硬件,如果有人关心的话)。我想循环并检查条件,并在条件为假时更改标签背景颜色。

我已经尝试了这十种不同的方式,但是我不断抛出异常,因为我创建的临时变量不会对其中一个标签名称进行赋值。

'Table for correct switch module for corresponding actuator
Dim ActLabelLookup(,) As Label =
{{MTA91, MTA92, MTA93, MTA94, MTA95, MTA96, MTA97, MTA98, MTA99, MTA100},
{MTA81, MTA82, MTA83, MTA84, MTA85, MTA86, MTA87, MTA88, MTA89, MTA90},
{MTA71, MTA72, MTA73, MTA74, MTA75, MTA76, MTA77, MTA78, MTA79, MTA80},
{MTA61, MTA62, MTA63, MTA64, MTA65, MTA66, MTA67, MTA68, MTA69, MTA70},
{MTA51, MTA52, MTA53, MTA54, MTA55, MTA56, MTA57, MTA58, MTA59, MTA60},
{MTA41, MTA42, MTA43, MTA44, MTA45, MTA46, MTA47, MTA48, MTA49, MTA50},
{MTA31, MTA32, MTA33, MTA34, MTA35, MTA36, MTA37, MTA38, MTA39, MTA40},
{MTA21, MTA22, MTA23, MTA24, MTA25, MTA26, MTA27, MTA28, MTA29, MTA30},
{MTA11, MTA12, MTA13, MTA14, MTA15, MTA16, MTA17, MTA18, MTA19, MTA20},
{MTA1, MTA2, MTA3, MTA4, MTA5, MTA6, MTA7, MTA8, MTA9, MTA10}}

Private Sub UpdateActuatorStatus()
Dim X As Integer
Dim Y As Integer
Dim CurrAct As New Label

For X = 0 To (ActControl.MAX_X - 1)
    For Y = 0 To (ActControl.MAX_Y - 1)
        If TempFunctionalActuatorMatrix(X, Y) = False Then
            CurrAct = ActLabelLookup(X, Y)
            CurrAct.BackColor = Color.Firebrick
        End If
    Next
Next

End Sub

使用此代码,CurrAct永远不会被设置为任何东西。有人看到我做错了吗?

Screen Cap

2 个答案:

答案 0 :(得分:1)

你的数组没有被初始化(好吧,它是,但它是用nothings初始化的,因为标签在创建表单实例时没有任何东西)。

在解析之前尝试填充它(在Form Load或UpdateActuatorStatus中):

     ActLabelLookup =
{{MTA91, MTA92, MTA93, MTA94, MTA95, MTA96, MTA97, MTA98, MTA99, MTA100},
{MTA81, MTA82, MTA83, MTA84, MTA85, MTA86, MTA87, MTA88, MTA89, MTA90},
{MTA71, MTA72, MTA73, MTA74, MTA75, MTA76, MTA77, MTA78, MTA79, MTA80},
{MTA61, MTA62, MTA63, MTA64, MTA65, MTA66, MTA67, MTA68, MTA69, MTA70},
{MTA51, MTA52, MTA53, MTA54, MTA55, MTA56, MTA57, MTA58, MTA59, MTA60},
{MTA41, MTA42, MTA43, MTA44, MTA45, MTA46, MTA47, MTA48, MTA49, MTA50},
{MTA31, MTA32, MTA33, MTA34, MTA35, MTA36, MTA37, MTA38, MTA39, MTA40},
{MTA21, MTA22, MTA23, MTA24, MTA25, MTA26, MTA27, MTA28, MTA29, MTA30},
{MTA11, MTA12, MTA13, MTA14, MTA15, MTA16, MTA17, MTA18, MTA19, MTA20},
{MTA1, MTA2, MTA3, MTA4, MTA5, MTA6, MTA7, MTA8, MTA9, MTA10}}

答案 1 :(得分:0)

将ActLabelLookup的成员级声明更改为:

Dim ActLabelLookup(,) As Label

在表单的加载事件处理程序中添加一行来初始化它:

ActLabelLookup(,) =
{{MTA91, MTA92, MTA93, MTA94, MTA95, MTA96, MTA97, MTA98, MTA99, MTA100},
{MTA81, MTA82, MTA83, MTA84, MTA85, MTA86, MTA87, MTA88, MTA89, MTA90},
{MTA71, MTA72, MTA73, MTA74, MTA75, MTA76, MTA77, MTA78, MTA79, MTA80},
{MTA61, MTA62, MTA63, MTA64, MTA65, MTA66, MTA67, MTA68, MTA69, MTA70},
{MTA51, MTA52, MTA53, MTA54, MTA55, MTA56, MTA57, MTA58, MTA59, MTA60},
{MTA41, MTA42, MTA43, MTA44, MTA45, MTA46, MTA47, MTA48, MTA49, MTA50},
{MTA31, MTA32, MTA33, MTA34, MTA35, MTA36, MTA37, MTA38, MTA39, MTA40},
{MTA21, MTA22, MTA23, MTA24, MTA25, MTA26, MTA27, MTA28, MTA29, MTA30},
{MTA11, MTA12, MTA13, MTA14, MTA15, MTA16, MTA17, MTA18, MTA19, MTA20},
{MTA1, MTA2, MTA3, MTA4, MTA5, MTA6, MTA7, MTA8, MTA9, MTA10}}