Results 1 to 4 of 4

Thread: [RESOLVED] label background colour

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2015
    Posts
    25

    Resolved [RESOLVED] label background colour

    I'm trying to get the colour of a label to change when the mouse is hovered over it. I've managed to do it however when the mouse leaves, rather than going back to the original colour of the background, it goes to a light grey colour. Any idea's why this is?

    Code:
     Private Sub LblNewuser_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LblNewuser.MouseEnter
            Dim lbl As Label = DirectCast(sender, Label)
            lbl.BackColor = Color.FromArgb(80, Color.CornflowerBlue)
    
        End Sub
    
        Private Sub LblNewuser_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LblNewuser.MouseLeave
            Dim lbl As Label = DirectCast(sender, Label)
            lbl.BackColor = Color.FromArgb(80, Color.Transparent)
    Name:  Form login screen.jpg
Views: 145
Size:  17.2 KB

  2. #2
    Fanatic Member
    Join Date
    Feb 2003
    Posts
    1,003

    Re: label background colour

    Just save the original background color into a variable saved at class level (meaning that the entire class can access it) and save to label's color before changing it when the mouse moves into it. Then change the color back to what's stored in the variable when the mouse leaves.
    Code:
    Public Class Form1
       Private LabelBackgroundColor As New Color
    
       Private Sub Label1_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.MouseEnter
          LabelBackgroundColor = Label1.BackColor
          Label1.BackColor = Color.CornflowerBlue
       End Sub
    
       Private Sub Label1_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.MouseLeave
          Label1.BackColor = LabelBackgroundColor
       End Sub
    End Class

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,242

    Re: label background colour

    I've encountered things like this in the past, and never spent the time to really diagnose what is going on. Is this the line that you are actually using to get the label back to the color it was?

    lbl.BackColor = Color.FromArgb(80, Color.Transparent)

    If so, why the 80? Wouldn't you want a fully transparent backcolor rather than a partially transparent one?
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2015
    Posts
    25

    Re: label background colour

    Ahhh thanks, that seems to have worked!

Posting Permissions

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



Click Here to Expand Forum to Full Width