I want all command buttons to change color from a VBA code

I have 50 generators, which is a lot of buttons. Its easy to strong arm this, and write a line specifying each one, but I'm sure there is an elegant solution where I could say:

For Each Commandbutton in ThisDisplay.objects

.BackColor = color1

.ForeColor = color2

next CommandButton

but for now I'm stuck with this ugliness:

  ButtonSR1.BackColor = color1

  ButtonSR1.ForeColor = color2

 

  ButtonSR2.BackColor = color1

  ButtonSR2.ForeColor = color2

 

  ButtonWR1.BackColor = color1

  ButtonWR1.ForeColor = color2

 

  ButtonWR2.BackColor = color1

  ButtonWR2.ForeColor = color2

 

  ButtonWR3.BackColor = color1

  ButtonWR3.ForeColor = color2

 

this goes on for a while, I'll spare you the rest...

  • is there anything i can do to draw attention to this? I've been struggling with it for a long time, and my code is repeated so many times. The biggest problem is that any time I make a change, I have to add/remove those lines in many different subs, and i am prone to missing things.

     

    Thank you in advance. I'm sorry if this question is stupid, but I don't know where else to look.

  • Hi Jennifer,

    I understand your frustration. Though your question is tagged with "PI ProcessBook", there is nothing about PI at play here. Your entire question boils down to VBA, and there are better forums to answer VBA questions than here at PI Square. I suggest you search on "vba loop over command buttons". You may get a lot of Excel hits, but that's perfectly okay. VBA is VBA be it in Excel, Word, ProcessBook, etc.

    Not to be cynical but "elegant" and "VBA" usually do not mix. VBA has a certain clunkiness to it, such as you needing to DIM any Object variable. And you may need to OleObject versus Object versus Control versus CommandButton etc.

    I do not know if this would work on a UserForm, but you could play around with:

    Public Sub ChangeButtonColors(color1 As OLE_COLOR, color2 As OLE_COLOR)
    
        Dim ctrl As Control
        Dim btn As CommandButton
    
        For Each ctrl In Me.Controls
            If Left(ctrl.Name, 6) = "Button" Then
                Set btn = ctrl 'Or is it ctrl.Object ?
                btn.ForeColor = color1
                btn.BackColor = color2
            End If
        Next
    
    End Sub

     

     

  • Your struggle is understood; hopefully, Rick's reply will get you pointed in the right direction. Since you are using ProcessBook, please understand it's days are numbered and consider moving to PI Vision.

  • thank you so much for your reply! I haven't been able to get this code to work because the method controls doesn't exist, but I'll take my question to another forum, as you recommend.

  • I really honestly do not know how to do this. I make new screens all the time, and this is still the only way i know how to do it. I can easily do it in excel. I just don't know how to do it in PI. I beg you, please, it doesn't seem to be a member of "thisdisplay" i don't know how to do it. I've tried for 2 years, and i have hundreds of very ugly processbooks. what am i doing wrong?!

     

    I've written this a hundred times, i just need to be able to cycle through the command buttons on a display, and it doesn't work the same way as excel. I can do it in excel, and i cannot do it in processbooks. I don't know if it's supposed to be a symbol, or what. please, someone please help. It grows exponentially worse.

  • still working on this.

    i can find the symbol, but i don't know why i can't change the color after that.

    my original method of buttonSR1.backcolor = color1 doesn't seem to work. backcolor doesn't exist as an option, instead there is backgroundcolor and fillcolor, and neither has any effect on the commandbutton. I can selected the button, but I can't change the color.

     

    this is the original code:

    Public Sub ShowPlantButton(inputplant)
    'Sets currently selected plant button to a different color.
    
        Dim plantName
        Dim color1, color2, color3, color4 As Single
        
        unit = Right(inputplant, 1)
        plantName = inputplant
        color1 = &HC0C0C0    ' inactive
        color2 = 0           ' inactive
        color3 = vbCyan    'text color active
        color4 = 0           'button color active
        
    'Switch all plant buttons back to normal.
    On Error Resume Next
        ButtonSR1.BackColor = color1
        ButtonSR2.BackColor = color1
        ButtonWR1.BackColor = color1
        ButtonWR2.BackColor = color1
        ButtonWR3.BackColor = color1
        ButtonSH.BackColor = color1
        ButtonSD.BackColor = color1
        ButtonGP.BackColor = color1
        ButtonEH1.BackColor = color1
        ButtonEHD.BackColor = color1
        ButtonEH2.BackColor = color1
        ButtonEH3.BackColor = color1
        ButtonEH4.BackColor = color1
        
        ButtonEFB1.BackColor = color1
        ButtonEFB2.BackColor = color1
        ButtonEFB3.BackColor = color1
        ButtonEFB4.BackColor = color1
        ButtonEFB5.BackColor = color1
        ButtonEFB6.BackColor = color1
        ButtonEFB7.BackColor = color1
        ButtonEFB8.BackColor = color1
        ButtonEFB9.BackColor = color1
        ButtonEFB10.BackColor = color1
        ButtonEFB11.BackColor = color1
        ButtonEFB12.BackColor = color1
        
        ButtonGW1.BackColor = color1
        ButtonGW2.BackColor = color1
        ButtonGW3.BackColor = color1
        ButtonGW4.BackColor = color1
        ButtonGW5.BackColor = color1
        ButtonGW45.BackColor = color1
        ButtonNPT1.BackColor = color1
        
        ButtonHV1.BackColor = color1
        ButtonHV2.BackColor = color1
        ButtonHV3.BackColor = color1
        ButtonHV4.BackColor = color1
        ButtonHV5.BackColor = color1
        ButtonHV6.BackColor = color1
        ButtonHV7.BackColor = color1
        ButtonHV8.BackColor = color1
        ButtonHV9.BackColor = color1
        ButtonHV10.BackColor = color1
        
        ButtonPJ1.BackColor = color1
        ButtonPJ2.BackColor = color1
        ButtonPJ3.BackColor = color1
        
        ButtonGW.BackColor = color1
        ButtonSteam.BackColor = color1
        ButtonNPT_ST.BackColor = color1
        ButtonBarrett_ST.BackColor = color1
        ButtonPJ_ST.BackColor = color1
        ButtonEast.BackColor = color1
        
        ButtonMonthView.BackColor = color1
        ButtonBeginningofMonth.BackColor = color1
        ButtonMidnighttoMidnight.BackColor = color1
        ButtonRealTime.BackColor = color1
        ButtonWB.BackColor = color1
        
    'Set currently selected plant's button to different color
        Select Case plantName
            Case "EFB1"
                ButtonEFB1.BackColor = color3
            Case "EFB2"
                ButtonEFB2.BackColor = color3
            Case "EFB3"
                ButtonEFB3.BackColor = color3
            Case "EFB4"
                ButtonEFB4.BackColor = color3
            Case "EFB5"
                ButtonEFB5.BackColor = color3
            Case "EFB6"
                ButtonEFB6.BackColor = color3
            Case "EFB7"
                ButtonEFB7.BackColor = color3
            Case "EFB8"
                ButtonEFB8.BackColor = color3
            Case "EFB9"
                ButtonEFB9.BackColor = color3
                status = Trend.SetTraceScale(ScaleLow, ScaleHigh)
            Case "EFB10"
                ButtonEFB10.BackColor = color3
                status = Trend.SetTraceScale(ScaleLow, ScaleHigh)
            Case "EFB11"
                ButtonEFB11.BackColor = color3
                status = Trend.SetTraceScale(ScaleLow, ScaleHigh)
            Case "EFB12"
                ButtonEFB12.BackColor = color3
                status = Trend.SetTraceScale(ScaleLow, ScaleHigh)
            Case "WB"
                ButtonWB.BackColor = color3
                status = Trend.SetTraceScale(ScaleLow, ScaleHigh)
                'ButtonBarrettGT.BackColor = color1
            Case "SRM1"
                ButtonSR1.BackColor = color3
                ButtonSR1.ForeColor = color4
            Case "SRM2"
                ButtonSR2.BackColor = color3
                ButtonSR2.ForeColor = color4
            Case "WR1"
                ButtonWR1.BackColor = color3
                ButtonWR1.ForeColor = color4
            Case "WR2"
                ButtonWR2.BackColor = color3
                ButtonWR2.ForeColor = color4
            Case "WR3"
                ButtonWR3.BackColor = color3
                ButtonWR3.ForeColor = color4
            Case "SH"
                ButtonSH.BackColor = color3
                ButtonSH.ForeColor = color4
            Case "SD"
                ButtonSD.BackColor = color3
                ButtonSD.ForeColor = color4
            Case "GP"
                ButtonGP.BackColor = color3
                ButtonGP.ForeColor = color4
            Case "EH1"
                ButtonEH1.BackColor = color3
                ButtonEH1.ForeColor = color4
            Case "EHD"
                ButtonEHD.BackColor = color3
                ButtonEHD.ForeColor = color4
            Case "PJ1"
                ButtonPJ1.BackColor = color3
                ButtonPJ1.ForeColor = color4
            Case "PJ2"
                ButtonPJ2.BackColor = color3
                ButtonPJ2.ForeColor = color4
            'Case "PJ3"
                'ButtonPJ3.BackColor = color3
                'ButtonPJ3.ForeColor = color4
            Case "EH2"
                ButtonEH2.BackColor = color3
                ButtonEH2.ForeColor = color4
            Case "EH3"
                ButtonEH3.BackColor = color3
                ButtonEH3.ForeColor = color4
            Case "EH4"
                ButtonEH4.BackColor = color3
                ButtonEH4.ForeColor = color4
            
            Case "GW1"
                ButtonGW1.BackColor = color3
                    ScaleLow = -10
                    ScaleHigh = 30
            Case "GW2"
                ButtonGW2.BackColor = color3
            Case "GW3"
                ButtonGW3.BackColor = color3
            Case "GW45"
                ButtonGW45.BackColor = color3
            Case "GW4"
                ButtonGW4.BackColor = color3
            Case "GW5"
                ButtonGW5.BackColor = color3
            'Case "NPT1"
                'ButtonNPT1.BackColor = color3
            Case "HV1"
                ButtonHV1.BackColor = color3
            Case "HV2"
                ButtonHV2.BackColor = color3
            Case "HV3"
                ButtonHV3.BackColor = color3
            Case "HV4"
                ButtonHV4.BackColor = color3
            Case "HV5"
                ButtonHV5.BackColor = color3
            Case "HV6"
                ButtonHV6.BackColor = color3
            Case "HV7"
                ButtonHV7.BackColor = color3
            Case "HV8"
                ButtonHV8.BackColor = color3
            Case "HV9"
                ButtonHV9.BackColor = color3
            Case "HV10"
                ButtonHV10.BackColor = color3
                
    
                
            Case "NPT" & unit
                ButtonNPT_ST.BackColor = vbCyan
            Case "Bar" & unit
                ButtonBarrett_ST.BackColor = vbCyan
            Case "PJ" & unit
                ButtonPJ_ST.BackColor = vbCyan
            Case Else
            
            
        End Select
    '    Select Case unit
    '    Case "1"
    '        Button1.BackColor = vbCyan
    '    Case "2"
    '        Button2BackColor = vbCyan
    '    Case "3"
    '        Button3.BackColor = vbCyan
    '    Case "4"
    '        Button4.BackColor = vbCyan
    '    End Select
        
    End Sub

     and this is the way i would like it to work.

    Sub TESTFINDBUTTONS()
        Dim mySymbol As Symbol
        Dim i As Integer
        'Dim button As TBSymbol 'also tried oleobject, object, control, controls, PBobject, commandbutton, and many more.
        On Error GoTo errorhandler
        
        
        For Each mySymbol In ThisDisplay.Symbols
            
            X = mySymbol.Type
            Y = mySymbol.Name
            z = Left(mySymbol.Name, 6)
            
         If mySymbol.Type = 14 And z = "Button" Then
                Dim button As p: button = mySymbol
                button.BackColor = vbBlue
                'Set button = ThisDisplay.Controls(mySymbol.Name)
                'ThisDisplay.CommandButton(Y).BackColor = vbBlue
                ThisDisplay.Symbols(Y).BackgroundColor = vbBlue
                ThisDisplay.Symbols(Y).FillColor = vbBlue
                ThisDisplay.Symbols(Y).Selected = True
                'mySymbol.BackColor = vbBlue
                'ButtonSR1.BackColor = color1
                mySymbol.FillColor = vbBlue
                mySymbol.Selected = False
                Selected.BackColor = vbBlue
    ' .BackColor = vbBlue
         
         
         End If
         
    Next
    
    errorhandler:
    Resume Next
    End Sub