Button ForeColor switches from black to blue when click on.

TAPS_MikeDion

Well-known Member
Joined
Aug 14, 2009
Messages
622
Office Version
  1. 2011
Platform
  1. MacOS
Hi all,
I did a search on here, but didn't find what I'm looking for.

I currently have 16 (which may go up to ???) command buttons on a form, and I wish to have the forecolor change from black to blue (&H00FF0000&) when the user clicks on any given button. I was trying to avoid using 16 sets (or more) of .ForeColor commands per command button Sub to set all buttons to black except for the one clicked. :eek:

In a nut shell, all buttons would have their forecolor set to black, and only the clicked-on button forecolor switches to blue.

Thanks to anyone that can help! ;)

-Mike
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi Mike

You can define a new class for the buttons in this group of commandbuttons.

This is an example. Do the following test:

1 - create a new userform and insert some commandbuttons and other controls

2 - in some of the commandbuttons set the Tag property to "CButton"

3 - paste in the userform module:

Code:
Option Explicit
 
Private CButtonsColl As New Collection
Private CButtonLastClickedP As clsCButton
 
Private Sub UserForm_Initialize()
Dim cbutton As clsCButton
Dim ctrl As MSForms.Control
 
For Each ctrl In Me.Controls
    If TypeOf ctrl Is MSForms.CommandButton Then
        If ctrl.Tag = "CButton" Then
            Set cbutton = New clsCButton
            Set cbutton.CButtonObj = ctrl
            CButtonsColl.Add cbutton
        End If
    End If
Next ctrl
End Sub
 
Property Set CButtonLastClicked(cButtonLastClickedNew As clsCButton)
Set CButtonLastClickedP = cButtonLastClickedNew
End Property
 
Property Get CButtonLastClicked() As clsCButton
Set CButtonLastClicked = CButtonLastClickedP
End Property

4 - insert a new class module and change its name to "clsCButton"

5 - paste in the clsCButton class module

Code:
Option Explicit
 
Private WithEvents CButtonP As MSForms.CommandButton
 
Property Set CButtonObj(ctrlCButton As MSForms.CommandButton)
Set CButtonP = ctrlCButton
End Property
 
Property Let ForeColor(ForeColorNew)
CButtonP.ForeColor = ForeColorNew
End Property
 
Private Sub CButtonP_Click()
Dim cbutton As clsCButton
 
Set cbutton = UserForm1.CButtonLastClicked
If Not cbutton Is Nothing Then cbutton.ForeColor = &H80000012
CButtonP.ForeColor = &HFF0000
Set UserForm1.CButtonLastClicked = Me
End Sub


6 - Test this solution. Execute the userform and check that for all the buttons that you set the tag to CButton you get the behaviour you want: when you click on one of these buttons its forecolour becomes blue and the forecolor of the previous button clicked gets black again.
 
Last edited:
Upvote 0
S W E E T !!!
(y)

After making the minor changes needed to fit into my code, it works perfectly!

Thanks a lot, I really appreciate it.

-Mike
 
Upvote 0
I'm glad it helped. Using a class usually simplifies this type of problems, where you want a group of objects to have the same behaviour.
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,720
Members
448,986
Latest member
andreguerra

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top