Massimo D. N.
Massimo D. N.

Reputation: 316

Is there a way to change the color of 'n' labels together with one command?

Is there a way to change the color of 10 labels together with one command?

For example, instead of:

Label1.ForeColor = Color.Black
Label2.ForeColor = Color.Black
Label3.ForeColor = Color.Black
Label4.ForeColor = Color.Black
Label5.ForeColor = Color.Black
Label6.ForeColor = Color.Black
Label7.ForeColor = Color.Black

I would like to use only one command to change the ForeColor. For example, instead of Label1 it would be LabelX.

Upvotes: 1

Views: 468

Answers (4)

Bugs
Bugs

Reputation: 4489

This is a recursive solution which will address Label controls being placed within containers such as a GroupBox or a Panel.

I will attempt to show you the difference to explain why a recursive method may be required. I'm using Color.Red to illustrate the difference. You would change the code to Color.Black.

The following code will set the ForeColor property to Color.Red on Label controls that have been placed on your form:

For Each lbl As Label In Me.Controls.OfType(Of Label)()
    lbl.ForeColor = Color.Red
Next

This is what it will look like:

enter image description here

You can see here that only Label4 was set.

The following code will set all Label controls:

Private Sub SetAllLabelsForeColor(ByVal parent As Control)

    For Each c As Control In parent.Controls
        If TypeOf (c) Is Label Then
            c.ForeColor = Color.Red
        Else
            If c.HasChildren Then
                SetAllLabelsForeColor(c)
            End If
        End If
    Next

End Sub

You can then call this with the following code:

SetAllLabelsForeColor(Me)

This is a screenshot of the outcome:

enter image description here

Upvotes: 3

Mederic
Mederic

Reputation: 2019

You can just loop through all the controls of type label. This should do the trick.

'For each control in the form
For Each ctrl As Control In Me.Controls
    'If its of type label
    If TypeOf ctrl Is Label Then
        'Change the color
        ctrl.ForeColor = Color.Black
    End If
Next

Edited like Vincent suggested so we don't need to declare ctr before.

As Bugs suggested here is an even shorter option:

For Each ctr In Me.Controls.OfType(Of Label)
    ctr.ForeColor = Color.Black
Next

Upvotes: 5

Alessandro Mandelli
Alessandro Mandelli

Reputation: 581

For i As Integer = 1 To 7
    Dim xL As Label = DirectCast(Controls("Label" & i.ToString), Label)
    xL.ForeColor = Color.Black
Next

Upvotes: 1

Visual Vincent
Visual Vincent

Reputation: 18310

You can make a loop that goes from 1 to X and get every label by name by concatenating the word Label with X.

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

Usage:

SetLabelRangeColor(<end label no.>, <color>)
'Or:
SetLabelRangeColor(<start label no.>, <end label no.>, <color>)

Usage examples:

'Label 1-6:
SetLabelRangeColor(6, Color.Red)

'Label 4-9:
SetLabelRangeColor(4, 9, Color.Red)

Upvotes: 1

Related Questions