你可以做一個循環,從1到X,並通過與X串聯詞Label
按名稱獲取每個標籤。
Private Sub SetLabelRangeColor(ByVal [End] As Integer, ByVal Color As Color)
SetLabelRangeColor(1, [End], Color)
End Sub
Private Sub SetLabelRangeColor(ByVal Start As Integer, ByVal [End] As Integer, ByVal Color As Color)
If Start > [End] Then Throw New ArgumentOutOfRangeException
For x = Start To [End]
Dim TargetLabel As Label = TryCast(Me.Controls("Label" & x), Label)
If TargetLabel IsNot Nothing Then
TargetLabel.ForeColor = Color
End If
Next
End Sub
用法:個
SetLabelRangeColor(<end label no.>, <color>)
'Or:
SetLabelRangeColor(<start label no.>, <end label no.>, <color>)
使用示例:
'Label 1-6:
SetLabelRangeColor(6, Color.Red)
'Label 4-9:
SetLabelRangeColor(4, 9, Color.Red)