Author Topic: Changing color of multiple labels at once  (Read 1044 times)

MrPike

  • Sr. Member
  • ****
  • Posts: 297
    • View Profile
Changing color of multiple labels at once
« on: December 09, 2015, 08:06:48 PM »
Hi, I'm looking for some code help that will allow me to change the SelectColor of many BasicLabels at once.  I have a check box that when unchecked I want basiclabels 1 thru 23 to be color 1.  I know I can list each label in an If Then statement but was hoping to do this with less code, maybe in a line or two. Thanks in advance!

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: Changing color of multiple labels at once
« Reply #1 on: December 09, 2015, 08:31:45 PM »
For each control you want to change, set the Tag property to 1, then use this code:
Code: [Select]
    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked Then
            For i = 0 To Me.Controls.Count - 1
                If Me.Controls(i).Tag = "1" Then
                    Me.Controls(i).BackColor = Color.Green
                End If
            Next
        End If
    End Sub

MrPike

  • Sr. Member
  • ****
  • Posts: 297
    • View Profile
Re: Changing color of multiple labels at once
« Reply #2 on: December 09, 2015, 09:38:26 PM »
Thanks Archie, I'm actually trying to select the color1 property of the basiclabel.  Changing the backcolor does not accomplish what I need.  I attached a pic and intellisense does not like it since color1 is not a member of 'System.Windows.Forms.control'.  Any other tricks?  Thanks 

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: Changing color of multiple labels at once
« Reply #3 on: December 10, 2015, 04:26:39 AM »
I assume you mean a BasicIndicator since the BasicLabel does not have a Color1 Property.
Code: [Select]
    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked Then
            For i = 0 To Me.Controls.Count - 1
                If Me.Controls(i).Tag = "1" Then
                    If TypeOf (Me.Controls(i)) Is AdvancedHMIControls.BasicIndicator Then
                        Dim c As AdvancedHMIControls.BasicIndicator = DirectCast(Me.Controls(i), AdvancedHMIControls.BasicIndicator)
                        c.Color1 = Color.Green
                    End If
                End If
            Next
        End If
    End Sub
« Last Edit: December 10, 2015, 04:31:28 AM by Archie »

MrPike

  • Sr. Member
  • ****
  • Posts: 297
    • View Profile
Re: Changing color of multiple labels at once
« Reply #4 on: December 10, 2015, 04:33:40 PM »
Yes. You are correct. I'm using the BasicIndicator. This works great. Sure saves a lot of typing. Thanks.