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...

Parents
  • 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

     

     

  • 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.

Reply
  • 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.

Children
No Data