Fuzydragon Fuzydragon - 4 months ago 20
Vb.net Question

VB only one If statement working in timer

I've been looking around and I haven't seen anyone with this problem and I need some help with this. I'm working on an alarm screen with several buttons that will show the state each alarm is in by color. I constantly check the state using a timer that looks at an array of incoming data from a PLC. When I run the code, only the first button changes color, none of the others do anything. I'm not sure what I have to do. Here is a sample of what I'm doing

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


Any help would be greatly appreciated. Thanks in advance.

Answer

You can use a dictionary for this purpose, to eliminate the extra if statements.

Here's some sample code that you could use:

    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))

That will spit out the color associated with the number. Each block of 4 else/ifs gets reduced to a single line. You can even make dictionaries of arbitrary keys, so you could have Buttons and Alarms linked together.

Comments