Changing BackColor of ToggleButton produces a muddy grey color

I

IanKR

I have a ToggleButton on a worksheet in a project I'm developing for a
cricket scorebook. I'd like it to change colour when pressed. This is my
code:

Private Sub togNoBall_Click()
With togNoBall
'when not pressed
If .Value = False Then
.BackColor = &H8000000F 'grey = default button colour
End If
'when pressed
If .Value = True Then
.BackColor = RGB(255, 0, 0) 'bright red
'[I shall eventually put some other code in here, to fire up the
No Ball Userform]
End If
End With
End Sub

I want it to change to red when clicked, and back to grey when "unclicked"
(or not clicked at all). The problem is, it does change colour, but not to
the bright, solid, vivid red that I'd expect. It changes to a sort of pink -
like if the red is mixed with grey. When it is clicked (and is the pink/grey
colour), and I then click on it again, before it changes back to grey, but
while the click is held down, it does change to the red colour I'd like it
to be, when clicked. A similar thing happens when I use other colours.

Is this due to some Windows appearance setting on my PC, outside of Excel?

Thanks

Ian
 
P

Peter T

When an ActiveX toggle button is depressed it displays a dithered
combination of white and the backcolor in alternate pixels. I don't know of
any direct way to make it a sold color (picture perhaps).

There are various ways you could simulate a depressed button. One is to use
the brick like autoshape from the "Basic shapes" menu (like a double
rectangle). Add some text and centralise vertically & horizontally

Assign to a macro and do something like

' in a normal module
Sub myToggle()
Dim bOldState As Boolean
Dim nFrClr As Long, nBkClr As Long
Dim sCaller As String
Dim shp As Shape
sCaller = Application.Caller

Set shp = ActiveSheet.Shapes(sCaller)
bOldState = shp.VerticalFlip
shp.Flip msoFlipHorizontal
shp.Flip msoFlipVertical

If bOldState Then
nFrClr = RGB(220, 220, 220)
nBkClr = vbBlack
Else
nFrClr = RGB(255, 0, 0)
nBkClr = vbWhite
End If

shp.Fill.ForeColor.RGB = nFrClr
shp.TextFrame.Characters.Font.Color = nBkClr

If bOldState Then
' code
Else
' code
End If
End Sub


Regards,
Peter T
 
I

IanKR

Many thanks, Peter, this works well. Interestingly, until I added the button
text, a message box titled "Microsoft Visual Basic" appeared every time I
clicked on the button, with just 400 on it! There was an OK button and a
Help button, but the Help button didn't do anything when I clicked it. But
when I added the text (and after I deleted the text), this no longer
appeared.

Ian
When an ActiveX toggle button is depressed it displays a dithered
combination of white and the backcolor in alternate pixels. I don't know
of any direct way to make it a sold color (picture perhaps).

There are various ways you could simulate a depressed button. One is to
use the brick like autoshape from the "Basic shapes" menu (like a double
rectangle). Add some text and centralise vertically & horizontally

Assign to a macro and do something like

' in a normal module
Sub myToggle()
Dim bOldState As Boolean
Dim nFrClr As Long, nBkClr As Long
Dim sCaller As String
Dim shp As Shape
sCaller = Application.Caller

Set shp = ActiveSheet.Shapes(sCaller)
bOldState = shp.VerticalFlip
shp.Flip msoFlipHorizontal
shp.Flip msoFlipVertical

If bOldState Then
nFrClr = RGB(220, 220, 220)
nBkClr = vbBlack
Else
nFrClr = RGB(255, 0, 0)
nBkClr = vbWhite
End If

shp.Fill.ForeColor.RGB = nFrClr
shp.TextFrame.Characters.Font.Color = nBkClr

If bOldState Then
' code
Else
' code
End If
End Sub


Regards,
Peter T


IanKR said:
I have a ToggleButton on a worksheet in a project I'm developing for a
cricket scorebook. I'd like it to change colour when pressed. This is my
code:

Private Sub togNoBall_Click()
With togNoBall
'when not pressed
If .Value = False Then
.BackColor = &H8000000F 'grey = default button colour
End If
'when pressed
If .Value = True Then
.BackColor = RGB(255, 0, 0) 'bright red
'[I shall eventually put some other code in here, to fire up
the No Ball Userform]
End If
End With
End Sub

I want it to change to red when clicked, and back to grey when
"unclicked" (or not clicked at all). The problem is, it does change
colour, but not to the bright, solid, vivid red that I'd expect. It
changes to a sort of pink - like if the red is mixed with grey. When it
is clicked (and is the pink/grey colour), and I then click on it again,
before it changes back to grey, but while the click is held down, it does
change to the red colour I'd like it to be, when clicked. A similar thing
happens when I use other colours.

Is this due to some Windows appearance setting on my PC, outside of
Excel?

Thanks

Ian
 
I

IanKR

Interestingly, until I added the button text, a message box titled
"Microsoft Visual Basic" appeared every time I clicked on the button, with
just 400 on it! There was an OK button and a Help button, but the Help
button didn't do anything when I clicked it. But when I added the text
(and after I deleted the text), this no longer appeared.

Just twigged why (very slow!). Of course, the code changes text colour, and
if there's no text... But it's an unusual VB error message, as it doesn't
halt the code and go into debug mode. That's what confused me.
 
P

Peter T

For me, if the AutoShape does not have a TextFrame I get error 1004 unable
to set the color property of the font class. I tested in XL2003 and some
earlier versions. Which version are you using.

BTW it's the lack of a textframe rather than no text that trigger the
anticipated error, at least for me.

Small point of style I forgot to mention last time, to make the 'Bevel'
autoshape look more like a button format with 'no line'.

Regards,
Peter T


Peter T said:
When an ActiveX toggle button is depressed it displays a dithered
combination of white and the backcolor in alternate pixels. I don't know
of
any direct way to make it a sold color (picture perhaps).

There are various ways you could simulate a depressed button. One is to
use
the brick like autoshape from the "Basic shapes" menu (like a double
rectangle). Add some text and centralise vertically & horizontally

Assign to a macro and do something like

' in a normal module
Sub myToggle()
Dim bOldState As Boolean
Dim nFrClr As Long, nBkClr As Long
Dim sCaller As String
Dim shp As Shape
sCaller = Application.Caller

Set shp = ActiveSheet.Shapes(sCaller)
bOldState = shp.VerticalFlip
shp.Flip msoFlipHorizontal
shp.Flip msoFlipVertical

If bOldState Then
nFrClr = RGB(220, 220, 220)
nBkClr = vbBlack
Else
nFrClr = RGB(255, 0, 0)
nBkClr = vbWhite
End If

shp.Fill.ForeColor.RGB = nFrClr
shp.TextFrame.Characters.Font.Color = nBkClr

If bOldState Then
' code
Else
' code
End If
End Sub


Regards,
Peter T


IanKR said:
I have a ToggleButton on a worksheet in a project I'm developing for a
cricket scorebook. I'd like it to change colour when pressed. This is my
code:

Private Sub togNoBall_Click()
With togNoBall
'when not pressed
If .Value = False Then
.BackColor = &H8000000F 'grey = default button colour
End If
'when pressed
If .Value = True Then
.BackColor = RGB(255, 0, 0) 'bright red
'[I shall eventually put some other code in here, to fire up
the No Ball Userform]
End If
End With
End Sub

I want it to change to red when clicked, and back to grey when
"unclicked"
(or not clicked at all). The problem is, it does change colour, but not
to
the bright, solid, vivid red that I'd expect. It changes to a sort of
pink - like if the red is mixed with grey. When it is clicked (and is the
pink/grey colour), and I then click on it again, before it changes back
to
grey, but while the click is held down, it does change to the red colour
I'd like it to be, when clicked. A similar thing happens when I use other
colours.

Is this due to some Windows appearance setting on my PC, outside of
Excel?

Thanks

Ian
 
I

IanKR

Thanks. I'm on XL 2003, fully-patched.

Peter T said:
For me, if the AutoShape does not have a TextFrame I get error 1004 unable
to set the color property of the font class. I tested in XL2003 and some
earlier versions. Which version are you using.

BTW it's the lack of a textframe rather than no text that trigger the
anticipated error, at least for me.

Small point of style I forgot to mention last time, to make the 'Bevel'
autoshape look more like a button format with 'no line'.

Regards,
Peter T


Peter T said:
When an ActiveX toggle button is depressed it displays a dithered
combination of white and the backcolor in alternate pixels. I don't know
of
any direct way to make it a sold color (picture perhaps).

There are various ways you could simulate a depressed button. One is to
use
the brick like autoshape from the "Basic shapes" menu (like a double
rectangle). Add some text and centralise vertically & horizontally

Assign to a macro and do something like

' in a normal module
Sub myToggle()
Dim bOldState As Boolean
Dim nFrClr As Long, nBkClr As Long
Dim sCaller As String
Dim shp As Shape
sCaller = Application.Caller

Set shp = ActiveSheet.Shapes(sCaller)
bOldState = shp.VerticalFlip
shp.Flip msoFlipHorizontal
shp.Flip msoFlipVertical

If bOldState Then
nFrClr = RGB(220, 220, 220)
nBkClr = vbBlack
Else
nFrClr = RGB(255, 0, 0)
nBkClr = vbWhite
End If

shp.Fill.ForeColor.RGB = nFrClr
shp.TextFrame.Characters.Font.Color = nBkClr

If bOldState Then
' code
Else
' code
End If
End Sub


Regards,
Peter T


IanKR said:
I have a ToggleButton on a worksheet in a project I'm developing for a
cricket scorebook. I'd like it to change colour when pressed. This is my
code:

Private Sub togNoBall_Click()
With togNoBall
'when not pressed
If .Value = False Then
.BackColor = &H8000000F 'grey = default button colour
End If
'when pressed
If .Value = True Then
.BackColor = RGB(255, 0, 0) 'bright red
'[I shall eventually put some other code in here, to fire up
the No Ball Userform]
End If
End With
End Sub

I want it to change to red when clicked, and back to grey when
"unclicked"
(or not clicked at all). The problem is, it does change colour, but not
to
the bright, solid, vivid red that I'd expect. It changes to a sort of
pink - like if the red is mixed with grey. When it is clicked (and is
the
pink/grey colour), and I then click on it again, before it changes back
to
grey, but while the click is held down, it does change to the red colour
I'd like it to be, when clicked. A similar thing happens when I use
other
colours.

Is this due to some Windows appearance setting on my PC, outside of
Excel?

Thanks

Ian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top