Cary Bondoc - 1 year ago 86

Vb.net Question

I have 20 labels with names like

`lbl_A1`

`lbl_A2`

`lbl_A3`

`lbl_A4`

`lbl_B1`

`lbl_B2`

`lbl_B3`

`lbl_B4`

`lbl_E4`

`0 = red, 1 = yellow, 2 = green`

`'I arrayed all the values of it, each item contains its specific value`

Dim lightFunctions = New Integer() {a1_LightFunction, b1_LightFunction, c1_LightFunction, d1_LightFunction, e1_LightFunction _

, a2_LightFunction, b2_LightFunction, c2_LightFunction, d2_LightFunction, e2_LightFunction _

, a3_LightFunction, b3_LightFunction, c3_LightFunction, d3_LightFunction, e3_LightFunction _

, a4_LightFunction, b4_LightFunction, c4_LightFunction, d4_LightFunction, e4_LightFunction}

'Loop through each item of array and get the value

For Each lightFunctionsValue As Integer In lightFunctions

'Loop through each label in my form

For Each c As Label In Me.Controls.OfType(Of Label)()

'This is my problem, I don't know how to make this that if it detects that for example the label's name ends with A1 then it should get the value of a1_LightFunction the if it is 0 it should be red

If c.Name.EndsWith("") and lightFunctionsValue = 0 Then c.ForeColor = color.red

If c.Name.EndsWith("") and lightFunctionsValue = 1 Then c.ForeColor = color.yellow

If c.Name.EndsWith("") and lightFunctionsValue = 2 Then c.ForeColor = color.green

Next

Next

I believed that if I do it like that I can avoid so many if's condition like this

`If a1_LightFunction = 0 Then`

lbl_A1.ForeColor = Color.Red

End If

If b1_LightFunction = 0 Then

lbl_B1.ForeColor = Color.Red

End If

If c1_LightFunction = 0 Then

lbl_C1.ForeColor = Color.Red

End If

If d1_LightFunction = 0 Then

lbl_D1.ForeColor = Color.Red

End If

If e1_LightFunction = 0 Then

lbl_E1.ForeColor = Color.Red

End If

And so on and so forth until it reaches ....

If e4_LightFunction = 2 Then

lbl_E4.ForeColor = Color.Green

End If

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

All you may need is a little abstraction and a way to link labels with some sort of key:

```
Private lblCol As Dictionary(Of String, Label)
...
Dim lbls As Label() = {Label2, Label3, Label4, Label5}
Dim keys As String() = {"lbl_a1", "lbl_c1", "lbl_b4", "lbl_d3"}
lblCol = New Dictionary(Of String, Label)
For n As Int32 = 0 To keys.Count - 1
lblCol.Add(keys(n), lbls(n))
Next
```

Then a generic updater:

```
Private Sub UpdateLabel(lbl As Label, n As Int32)
Select Case n
Case 0
lbl.BackColor = Color.Red
Case 1
lbl.BackColor = Color.Yellow
Case 2
lbl.BackColor = Color.Green
End Select
End Sub
```

Loop thru all of them:

```
For Each kvp In lblCol
UpdateLabel(kvp.Value, RNG.Next(0, 3))
Next
```

To find them use the key, which in your original method was the name:

```
Dim find = "lbl_a1"
UpdateLabel(lblCol(find), RNG.Next(0, 3))
```

The key needs to be something simple from the DB to allow you to set up the link/map and make it simple to find the right control.

`RNG`

is a `Random`

object, you would use the DB values.

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**