我一直在四处寻找,但我还没有见过有这个问题的人,我需要一些帮助。我在一个带有几个按钮的警报屏幕上工作,这些按钮将显示每个警报的颜色状态。我经常使用定时器检查状态,该定时器查看来自PLC的输入数据数组。当我运行代码时,只有第一个按钮改变颜色,其他按钮都没有做任何事情。我不知道自己要做什么。以下是我正在做的事情的样本
Private Sub AlarmInformation_Load(sender As Object, e As EventArgs) Handles Me.Load
Timer1.Enabled = True
Timer1.Interval = 2000
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'Alarm0
If MainForm.inputDataReals(MainForm.AOEnumArray.Alarm0) = 0 Then
Button1.BackColor = Color.ForestGreen
ElseIf MainForm.inputDataReals(MainForm.AOEnumArray.Alarm0) = 2 Then
Button1.BackColor = Color.Red
ElseIf MainForm.inputDataReals(MainForm.AOEnumArray.Alarm0) = 3 Then
Button1.BackColor = Color.Yellow
ElseIf MainForm.inputDataReals(MainForm.AOEnumArray.Alarm0) = 4 Then
Button1.BackColor = Color.Gray
End If
'Alarm1
If MainForm.inputDataReals(MainForm.AOEnumArray.Alarm1) = 0 Then
Button1.BackColor = Color.ForestGreen
ElseIf MainForm.inputDataReals(MainForm.AOEnumArray.Alarm1) = 2 Then
Button1.BackColor = Color.Red
ElseIf MainForm.inputDataReals(MainForm.AOEnumArray.Alarm1) = 3 Then
Button1.BackColor = Color.Yellow
ElseIf MainForm.inputDataReals(MainForm.AOEnumArray.Alarm1) = 4 Then
Button1.BackColor = Color.Gray
End If
'Alarm2
If MainForm.inputDataReals(MainForm.AOEnumArray.Alarm2) = 0 Then
Button1.BackColor = Color.ForestGreen
ElseIf MainForm.inputDataReals(MainForm.AOEnumArray.Alarm2) = 2 Then
Button1.BackColor = Color.Red
ElseIf MainForm.inputDataReals(MainForm.AOEnumArray.Alarm2) = 3 Then
Button1.BackColor = Color.Yellow
ElseIf MainForm.inputDataReals(MainForm.AOEnumArray.Alarm2) = 4 Then
Button1.BackColor = Color.Gray
End If
'Alarm3
If MainForm.inputDataReals(MainForm.AOEnumArray.Alarm3) = 0 Then
Button1.BackColor = Color.ForestGreen
ElseIf MainForm.inputDataReals(MainForm.AOEnumArray.Alarm3) = 2 Then
Button1.BackColor = Color.Red
ElseIf MainForm.inputDataReals(MainForm.AOEnumArray.Alarm3) = 3 Then
Button1.BackColor = Color.Yellow
ElseIf MainForm.inputDataReals(MainForm.AOEnumArray.Alarm3) = 4 Then
Button1.BackColor = Color.Gray
End If
'Alarm4
If MainForm.inputDataReals(MainForm.AOEnumArray.Alarm4) = 0 Then
Button1.BackColor = Color.ForestGreen
ElseIf MainForm.inputDataReals(MainForm.AOEnumArray.Alarm4) = 2 Then
Button1.BackColor = Color.Red
ElseIf MainForm.inputDataReals(MainForm.AOEnumArray.Alarm4) = 3 Then
Button1.BackColor = Color.Yellow
ElseIf MainForm.inputDataReals(MainForm.AOEnumArray.Alarm4) = 4 Then
Button1.BackColor = Color.Gray
End If
非常感谢任何帮助。提前谢谢。
答案 0 :(得分:0)
您可以使用dictionary来消除额外的if
语句。
以下是您可以使用的示例代码:
Dim openWith As New Dictionary(Of Integer, Color)
' Add some elements to the dictionary. There are no
' duplicate keys, but some of the values are duplicates.
openWith.Add(0, Color.ForestGray)
openWith.Add(2, Color.Red)
'... and so on.
'Accessing the colors:
openWith(MainForm.inputDataReals(MainForm.AOEnumArray.Alarm0))
这会吐出与数字相关的颜色。 每个4个/ ifs块被减少为一行。你甚至可以制作任意键的字典,这样就可以将Buttons和Alarms链接在一起。