I have a code where according to the result string of numbers I assign to the Label / Label a corresponding color, but I would like to reduce that code:
to a reduced code (understanding the second line is not correct):
You can use the property Controls
of the form to reference controls by name:
this.Controls["lblNumero" + resultado].ForeColor = // ...
Take into account that the instance that returns you is of type Control
, so if you need to work with a specific property at Label
, you would have to make an explicit cast. But in this case, you only need access to the property ForeColor
which is a property common to all controls, so the cast is not necessary.