View Full Version : [SOLVED] change colour of lbl when hovvered

11-26-2013, 05:08 AM
Good afternoon,

I have seen various Userforms when a lbl is hoverred over with the mouse it changes colour.

How would I even code this?

ive been shownt he code

Dim oButton As Object

For Each oButton In oform.Controls
With oButton
If Left(.Tag, 3) = "btn" Then
.BackColor = Slate
End If
End With
Next oButton

If IsMissing(cButton) = False Then
If cButton Is Nothing Then
cButton.BackColor = blue
End If
End If

End Function

but for some reason it doesn't reset when mouse is not hovered over.

11-26-2013, 05:21 AM
George, I can't doublecheck my gut right now, but if it is possible, you would use the MouseOver Event.

11-26-2013, 05:33 AM
You can also put another slightly larger blank label control beneath the label in question and use its Mousemove event to reset the colour of the primary label.

11-26-2013, 05:35 AM
hmm tried that(SamT's). nothing happens... ive managed to get it to highlight using

Private Sub lblOK_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Call fHighlightButton(Me, Me.lblOK)
End Sub

and the fHighlightButton is the code in the previous post.

the problem I have is that I have several labels that would need to change. (links to a different frame etc) and I was hoping not to have to build more labels than i needed to and that there was a code out there i could have to adapt to each label.

11-26-2013, 05:54 AM
I think

If Left(.Tag, 3) = "btn" Then .BackColor = Slate
End If

maybe the problem.. What exactly is this asking.. because if its asking "if name of label is btn" in tag then this might explain why it's not "cancelling".

11-26-2013, 06:35 AM
No - it is asking if the first 3 letters of the Tag property are "btn"

11-26-2013, 06:35 AM
wow holy moly.. i figured my own problem, (basically the above note.. i neeed to add btn in the tag!)

Thanks for the alternative option aflatoon I will consider that for my smaller projects.

11-26-2013, 06:47 AM
wow holy moly.. i figured my own problem

Glad to hear it. Satisfying, isn't it? :)

Kenneth Hobs
11-26-2013, 06:56 AM
Tag is a Property as is Name. I am not sure why one would use "btn" as a prefix string in a Tag property. Most would use "btn" as the prefix for a Command Button's Name property.

Try explaining in words what you want. One can easily use the MouseMove event to set the label properties from the activecontrol, that label, and then set all others back to a default. Or, just use the MouseMove event for the Userform to reset all label properties to a default. It just depends on what you want.

If Left(.Name, 3) = "btn" Then .BackColor = Slate
or more likely

If Left(.Name, 3) = "lbl" Then .BackColor = Slate

Of course the Tag property is ideal for many situations.

11-27-2013, 04:50 PM
FWIW and in case you're still interested, I think that the MouseOver event would still be the most logical approach.

This fragment initializes the label BG to yellow, and when you move the mouse with into the UF, the label's BG is goes red.

Mousing into the label makes the BG green until you leave the label and then it's BG returns to red.

This is for one control, but you could create a class and use With Events to be generic. Personally, if I only had 4-5 controls, I think 4-5 mostly duplicate even handlers would be easier

Option Explicit
Private Sub CommandButton1_Click()
Unload Me
End Sub

Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Me.Label1.BackColor = vbGreen
End Sub
Private Sub UserForm_Activate()
Me.Label1.BackColor = vbYellow
End Sub
Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Me.Label1.BackColor = vbRed
End Sub


11-28-2013, 12:46 AM
Thanks for the Yellow, Green & Red MouseMove Code.

11-28-2013, 06:48 AM
Simple demo xlsm