Consulting

Results 1 to 12 of 12

Thread: change colour of lbl when hovvered

  1. #1

    change colour of lbl when hovvered

    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
        Else
            cButton.BackColor = blue
        End If
    End If
    
    
    End Function
    but for some reason it doesn't reset when mouse is not hovered over.
    Last edited by CuriousGeorg; 11-26-2013 at 05:27 AM.

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    George, I can't doublecheck my gut right now, but if it is possible, you would use the MouseOver Event.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    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.
    Be as you wish to seem

  4. #4
    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.

  5. #5
    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".

  6. #6
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    No - it is asking if the first 3 letters of the Tag property are "btn"
    Be as you wish to seem

  7. #7
    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.

  8. #8
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Quote Originally Posted by CuriousGeorg View Post
    wow holy moly.. i figured my own problem
    Glad to hear it. Satisfying, isn't it?
    Be as you wish to seem

  9. #9
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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.

  10. #10
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    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()
        Me.Hide
        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


    Paul

  11. #11
    VBAX Regular
    Joined
    Oct 2013
    Posts
    27
    Location
    Thanks for the Yellow, Green & Red MouseMove Code.

  12. #12
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Simple demo xlsm

    Paul
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •