Results 1 to 10 of 10

Thread: Why Commandbuttons are not caught in the Me.controls collection

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    562

    Why Commandbuttons are not caught in the Me.controls collection

    I have the following code:
    Code:
       Dim ctrl As Control
       
       On Error Resume Next
       For Each ctrl In Me.Controls
          ctrl.BackColor = 15136253
       Next
    And when I run it, it changes the backcolor of everything on the form except commandbuttons.
    All the commandbuttons remain in the color that they had at design time (gray)

    Why is it that the above code does not catch commandbuttons?
    And how can I fix it to catch commandbuttons?
    Thanks.

  2. #2
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,346

    Re: Why Commandbuttons are not caught in the Me.controls collection

    Set the CommandButton Style to "Graphical" and use this code! Adjust it to suit your needs.

    Code:
    Private Const CC_FULLOPEN = &H2
    Private Const CC_RGBINIT = &H1
    Private Type CHOOSECOLOR
        lStructSize As Long
        hwndOwner As Long
        hInstance As Long
        rgbResult As Long
        lpCustColors As Long
        flags As Long
        lCustData As Long
        lpfnHook As Long
        lpTemplateName As String
    End Type
    Private Declare Function CHOOSECOLOR Lib "comdlg32.dll" Alias "ChooseColorA" _
                (pChoosecolor As CHOOSECOLOR) As Long
    Private Sub Command1_Click()
     Colour
    End Sub
    
    
    'Apply colour to forms
    Public Sub Colour()
        Static lngCustom(15) As Long
        Static lngCurrent    As Long
        Dim objForm          As Form
        Dim Ctrl             As Control
        Dim uCol             As CHOOSECOLOR
    
        With uCol
            .lStructSize = LenB(uCol)
            .hwndOwner = Form1.hWnd
            .lpCustColors = VarPtr(lngCustom(0))
            .rgbResult = lngCurrent
            .flags = CC_FULLOPEN Or CC_RGBINIT
        End With
    
        If CHOOSECOLOR(uCol) Then
            lngCurrent = uCol.rgbResult
            If uCol.rgbResult = vbBlack Then Exit Sub
    
            On Error Resume Next
    
            For Each objForm In Forms
               'Customise the colour of the program
                'Uncomment to change form colour
                'objForm.BackColor = uCol.rgbResult
    
                For Each Ctrl In objForm.Controls
                   'Apply color to controls
                    Select Case TypeName(Ctrl)
                        Case "CommandButton" ' "Label", "OptionButton", "Frame", "PictureBox", "Slider", "Strip"
                            Ctrl.BackColor = uCol.rgbResult
                    End Select
                Next Ctrl
            Next objForm
        End If
    End Sub
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  3. #3
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,277

    Re: Why Commandbuttons are not caught in the Me.controls collection

    The code in the OP does hit the command buttons but by default you can't set the backcolor of the button, even if you change it in the designer the button will remain the same color unless you change it to graphical.

    The code in the OP will work just fine if the buttons are set to graphical.

  4. #4
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,346

    Re: Why Commandbuttons are not caught in the Me.controls collection

    Quote Originally Posted by DataMiser View Post
    The code in the OP does hit the command buttons but by default you can't set the backcolor of the button, even if you change it in the designer the button will remain the same color unless you change it to graphical.

    The code in the OP will work just fine if the buttons are set to graphical.
    Also, it appears that "15136253" is the default button/form colour?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  5. #5
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,439

    Re: Why Commandbuttons are not caught in the Me.controls collection

    NW says: Also, it appears that "15136253" is the default button/form colour?

    NO, it is a beige color. (at least here in the States!) :-)

  6. #6
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    804

    Re: Why Commandbuttons are not caught in the Me.controls collection

    The default color of a Button is &H8000000F& (vbButtonFace), which is a system color.
    When the GetSysColor api call is used, that translates to &HC8D0D4 or 13160660 in decimal.
    Code:
    Private Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long
    Private Function ConvertSysColor(ByVal lColor As Long) As Long
     If lColor < 0 Then
      lColor = GetSysColor(lColor And &HFF&)
     End If
     ConvertSysColor = lColor
    End Function

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,277

    Re: Why Commandbuttons are not caught in the Me.controls collection

    The default color may be different if you have a different theme active.

  8. #8
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,439

    Re: Why Commandbuttons are not caught in the Me.controls collection

    Yeah....I knew that, but was just picking on NW. :-)

  9. #9
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,346

    Re: Why Commandbuttons are not caught in the Me.controls collection

    Quote Originally Posted by SamOscarBrown View Post
    NW says: Also, it appears that "15136253" is the default button/form colour?

    NO, it is a beige color. (at least here in the States!) :-)
    Ah, it might just be my eyes because I didn't notice much of a change in the colour.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  10. #10
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,439

    Re: Why Commandbuttons are not caught in the Me.controls collection

    Or, it could be mine.....I'm sure I got you by a few years!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width