Background color change on button in Userform


Posted by WangYa on January 15, 2002 11:39 PM

How can one change the background color of a button when one hovers over it with the mouse, while it returns to normal when the mouse does not hover over it anymore?

Posted by Robb on January 16, 2002 2:05 AM

Wang Ya

There may be a more direct way in later versions, but I would suggest using the MouseMove event. You will need to set the colour of the button
by useing the MouseMove event for the command button and then change it back using the MouseMove event for the UserForm. To illustrate, try this:

- Open a new workbook
- Open VBE
- Add a UserForm named UserForm1
- Add a CommandButton named CommandButton1
- Paste this code in the code module for the UserForm
- Run the UserForm
- Move your mouse pointer on and off the button - it should change to Red whilst the mouse button is over it and back to grey when the mouse pointer
returns to being over the UserForm

Private Sub CommandButton1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
CommandButton1.BackColor = 251

End Sub

Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
CommandButton1.BackColor = -2147483633
End Sub


Any help?

Regards

Robb

Posted by Joe Was on January 16, 2002 7:39 AM

Open Red, mouse-over green, go red on mouse gone

This is like Robb's code only the button is colored Bed on open and it changes color to light green with a mouse-over and I used Red, Green, Blue code which is easy to change RGB(Red, Green, Blue) where each value is 0 to 256.

Private Sub CommandButton1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
'On mouse over color green.
CommandButton1.BackColor = RGB(122, 555, 100)
End Sub

Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
'On mouse over = gone color red.
CommandButton1.BackColor = RGB(256, 0, 0)
End Sub

Private Sub UserForm_Activate()
'On form open color red.
CommandButton1.BackColor = RGB(256, 0, 0)
End Sub

The above code is attached to the "View Code" page of the form's Command button.

To open the form to see the button, activate the UserForm with this code put on the sheet module.

Sub myForm()
ActiveSheet.Select
UserForm1.Show
End Sub

You can run this as a hot-key or attach it to a form button. JSW

Posted by Juan Pablo G. on January 16, 2002 8:11 AM

Re: Open Red, mouse-over green, go red on mouse gone

I'd only like to make one more suggestion, to avoid a little screen flickering. Put an IF condition before, something like:

Private Sub CommandButton1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
'On mouse over color green.
If CommandButton1.BackColor <> RGB(122, 255, 0) Then _
CommandButton1.BackColor = RGB(122, 255, 100)
End Sub

Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
'On mouse over = gone color red.
If CommandButton1.BackColor <> RGB(255, 0, 0) Then _
CommandButton1.BackColor = RGB(255, 0, 0)
End Sub

Private Sub UserForm_Activate()
'On form open color red.
If CommandButton1.BackColor <> RGB(255, 0, 0) Then _
CommandButton1.BackColor = RGB(255, 0, 0)
End Sub

Juan Pablo G. This is like Robb's code only the button is colored Bed on open and it changes color to light green with a mouse-over and I used Red, Green, Blue code which is easy to change RGB(Red, Green, Blue) where each value is 0 to 256.



Posted by Joe Was on January 16, 2002 10:41 AM

Your right Juan, this gives a softer transition, thank's JSW, nt