PDA

View Full Version : Change Color Of Labels As Mouse Moves (Using MouseOver event)



angsuman
01-13-2019, 06:48 AM
I have created an user form which has a frame. The frame contains several labels. My objective is to highlight the label when mouse points to that label but highlight should disappear once mouse moves to next label and the next label should be highlighted.

Following is my code:




Code is my user form:


Dim Labels() As New LblClass


Dim LabelCount As Long
Dim ctl As Control




' Create the Label objects
LabelCount = 0
For Each ctl In Frame1.Controls
If TypeName(ctl) = "Label" Then
LabelCount = LabelCount + 1
ReDim Preserve Labels(1 To LabelCount)
Set Labels(LabelCount).LabelGroup = ctl
End If
Next ctl



Created a class called LblClass and this class has following code:


Public WithEvents LabelGroup As MSForms.Label




Private Sub LabelGroup_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)




LabelGroup.ForeColor = vbWhite
LabelGroup.SpecialEffect = 1
LabelGroup.BackStyle = 1
LabelGroup.BackColor = &H808000

End Sub






With above code, when mouse moves over a label it is highlighted. Next when mouse moves over a new label, new label is highlighted and old label is still highlighted. This continues. How can I remove the highlight from my previous label when mouse moves over new label.

Thanks
Angsuman

p45cal
01-13-2019, 03:15 PM
Could you supply a file with this as you currently have it - save us having to setup our own files and guess (wrongly) what's in yours?

Paul_Hossler
01-15-2019, 07:13 PM
I had done something like that awhile ago, except I got carried away and did a number of different types of controls

I needed to polish an application to make it stand out -- the highlighting is pretty much just eye candy since it really is only part of the UI

I used the


Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

event to 'clear' the highlight color

The attached file has the idea. It's a little crude, but now that I've looked at it, I might play around some more

mikerickson
01-16-2019, 09:38 PM
Paul, very impresive.
If I could make one suggestion, adding this to the EnhancedCheckbox (and option button), so the background color reacts when a control is checked.

Private Sub CheckBoxEnhanced_Change()
Call CheckBoxEnhanced_MouseMove(Button:=1, Shift:=0, X:=0, Y:=0)
End Sub

Paul_Hossler
01-17-2019, 09:06 AM
Thanks and Thanks. Very nice addition

As I said, I started to play with it a little more, just for the heck of it (retired now so these are just fun little projects for me)

I tried to define a new base class clsEnhancedControl to build the 'real' enhanced controls off of, but the Control object doesn't expose the MouseMove and Change events that I wanted, so now I'm re-thinking the approach