我有20個標籤,與像lbl_A1
,lbl_A2
,lbl_A3
,lbl_A4
,lbl_B1
,lbl_B2
,lbl_B3
,lbl_B4
......直到lbl_E4
名。每個標籤forecolor應根據數據庫中的值0 = red, 1 = yellow, 2 = green
進行更改。比較標籤的名稱的最後2串前2字符串變量
'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
我相信,如果我這樣做,我能避免,如果這麼多的情況是這樣
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
爲什麼不寫一個小類將標籤鏈接到一個值。當你改變對象的值時,讓它設置顏色。 – Plutonix
對不起,這可能是一個基本問題。創建一個「將類標籤鏈接到值的小類」意味着我不需要將20個標籤拖放到GUI上? –
@Plutonix你的答案已被刪除先生,我仍然在嘗試它。 :( –