Results 1 to 25 of 25

Thread: how to change color of label when mouse hover?

  1. #1

    Thread Starter
    Registered User
    Join Date
    Mar 2013
    Posts
    9

    how to change color of label when mouse hover?

    Hi...

    I want to change label's color when mouse hover.I tried to run this code but i doesn't work... please help me.

    Private Sub lblMAUFilterARU_MouseHover(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lblMAUFilterARU.MouseHover

    lblMAUFilterARU.BackColor = System.Drawing.Color.Orange
    End Sub


    Thanks.

  2. #2
    Member
    Join Date
    Jun 2010
    Posts
    51

    Re: how to change color of label when mouse hover?

    Quote Originally Posted by elodea View Post
    Hi...

    I want to change label's color when mouse hover.I tried to run this code but i doesn't work... please help me.

    Private Sub lblMAUFilterARU_MouseHover(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lblMAUFilterARU.MouseHover

    lblMAUFilterARU.BackColor = System.Drawing.Color.Orange
    End Sub


    Thanks.
    Change your arguments to:

    Code:
        Private Sub lblMAUFilterARU_MouseHover(ByVal sender As Object, ByVal e As EventArgs) Handles lblMAUFilterARU.MouseHover
            lblMAUFilterARU.BackColor = System.Drawing.Color.Orange
        End Sub
    You also dont need to write System.Drawing.Color .... you can shorten that by writing Color.Orange (or whatever other color you want from the list)

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: how to change color of label when mouse hover?

    You have the wrong argument. To fix it, click on the lightning bolt in your properties window, then double click on the MouseHover event. This will automatically generate the correct event handler for you.

    Edit - Blaknite beat me to it!
    Last edited by dday9; May 22nd, 2013 at 08:50 AM. Reason: Ninja'd!
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4

    Re: how to change color of label when mouse hover?

    Just to also address this issue: The MouseHover event fires after a pre-determined timespan from the moment the mouse enters a control's bounds. A better way to do a label change would be to handle the MouseEnter and MouseLeave events for that control and change the color in those events. It would be much better as the results are instant rather than somewhat delayed.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: how to change color of label when mouse hover?

    Quote Originally Posted by formlesstree4 View Post
    Just to also address this issue: The MouseHover event fires after a pre-determined timespan from the moment the mouse enters a control's bounds. A better way to do a label change would be to handle the MouseEnter and MouseLeave events for that control and change the color in those events. It would be much better as the results are instant rather than somewhat delayed.
    Quite so. With the code as is, there's nothing to change the colour back again. Many people say "mouse hover" when what they actually mean is "mouse over". The MouseHover event is only raised when the mouse pointer stays still for a specific period of time. Presumably you want the colour to change whenever the mouse pointer is over the control and change back again when the mouse pointer is no longer over the control. That's what the MouseEnter and MouseLeave events give you notification of.

  6. #6
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: how to change color of label when mouse hover?

    Why do people still trap events on a control's container to alter their behavior ? This isn't VB6, we have inheritance now:-
    vbnet Code:
    1. Public Class LabelEx
    2.     Inherits Label
    3.  
    4.     Private g_bHovering As Boolean
    5.  
    6.     Public Sub New()
    7.         Me.HoverColor = Color.Empty
    8.         g_bHovering = False
    9.     End Sub
    10.  
    11.     Public Property HoverColor As Color
    12.  
    13.     Protected Overrides Sub OnMouseEnter(e As System.EventArgs)
    14.         If Not Me.HoverColor = Color.Empty Then
    15.             SetHoverState(True)
    16.         End If
    17.  
    18.         MyBase.OnMouseEnter(e)
    19.     End Sub
    20.  
    21.     Protected Overrides Sub OnMouseLeave(e As System.EventArgs)
    22.         SetHoverState(False)
    23.  
    24.         MyBase.OnMouseLeave(e)
    25.     End Sub
    26.  
    27.     Protected Overrides Sub OnPaintBackground(pevent As System.Windows.Forms.PaintEventArgs)
    28.         If g_bHovering Then
    29.             pevent.Graphics.FillRectangle(New SolidBrush(Me.HoverColor), Me.ClientRectangle)
    30.         Else
    31.             MyBase.OnPaintBackground(pevent)
    32.         End If
    33.     End Sub
    34.  
    35.  
    36.     Private Sub SetHoverState(ByVal mouseHovering As Boolean)
    37.         If g_bHovering = mouseHovering Then Return
    38.         g_bHovering = mouseHovering
    39.         Me.Refresh()
    40.     End Sub
    41.  
    42.     Private Function ShouldSerializeHoverColor() As Boolean
    43.         Return Not Me.HoverColor = Color.Empty
    44.     End Function
    45.  
    46. End Class

    The above is an inherited Label that can change color when the mouse pointer hovers over it. All you have to do is spread it on a container(forms or whatever) and set its HoverColor property.
    Last edited by Niya; May 22nd, 2013 at 01:26 PM.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  7. #7

    Re: how to change color of label when mouse hover?

    Quote Originally Posted by Niya View Post
    Why do people still trap events on a control's container to alter their behavior ? This isn't VB6, we have inheritance now:-
    There's more than one way to skin a cat you know!

    I don't see how "event trapping" is necessarily a VB6-era idea. Just because I can use a flat-head screwdriver on a Phillips head screw doesn't make it proper. It works, though.

    EDIT: Although, you did write a ton more code than trapping two events (2 events = approximately 3 lines of code each, with the method signature, the single line to change the color, and the ending mark, and then the event can then be applied to multiple labels instead of having to use a custom control to acquire the same thing).

  8. #8
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: how to change color of label when mouse hover?

    Quote Originally Posted by formlesstree4 View Post
    There's more than one way to skin a cat you know!

    I don't see how "event trapping" is necessarily a VB6-era idea. Just because I can use a flat-head screwdriver on a Phillips head screw doesn't make it proper. It works, though.

    EDIT: Although, you did write a ton more code than trapping two events (2 events = approximately 3 lines of code each, with the method signature, the single line to change the color, and the ending mark, and then the event can then be applied to multiple labels instead of having to use a custom control to acquire the same thing).
    You're not thinking big enough my friend .

    I remember years ago when I was a teenager. I was tasked to write a small program to do backups onto a rewritable CD. I wanted a distinct interface and one of the decisions I made was not to use standard Windows buttons, instead I opted to use Labels with a 3D border that responded to clicks by changing its back colour. For it to behave properly, I had to make it so that when the left mouse button was held down, the Label's back colour remained changed and only changed back when the mouse was release. That's two events I had to trap.

    Now this is a UI we're talking about so there's gonna be quite a few buttons needed. It was a royal pain having to individually trap all the MouseDown and MouseUp events of every single Label on every single Form to implement the color change. And Because a 3D border and an initial black background was also part of the look, it meant I had to also change two properties for every single label that acted as a button. This is a VB6-era thing because that's almost the only way you could do such a thing.

    Now, you may be wondering, why didn't I use a UserControl ? Well in my case, I wasn't aware of them at the time but even so, if a UserControl were to be used, you would have to spread the Label on the UserControl surface and reroute all the events from the Label on the UserControl's surface to pass through the UserControl which means you're still writing a lot of boilerplate. You would be redefining all the events(or maybe just the ones you want).

    These were your choices in VB6. What the OP did was about the only way you can change control behavior in VB6. We don't have to torture ourselves like that in VB.Net. You said that I wrote a "ton more code" but what I really did was write a ton more code only once. It would take 2 seconds to implement that Label anywhere in your program by simply spreading it as opposed to having to double click in the designer, and copy/paste into the event handler of every Label you want to have this hovering behavior. That takes a whole lot more than 2 seconds per Label. Think of all the time you would save if you're gonna use this all over the place.
    Last edited by Niya; May 22nd, 2013 at 02:06 PM.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  9. #9
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: how to change color of label when mouse hover?

    Oh and one more thing I forgot to mention. Because in this case we're co-coordinating between two events, there is a need to preserve state information. By using an inherited control, we can encapsulate state information inside it. Would would really want to muddy up your Form code with extra private fields to deal with this Label states ? And then consider having multiple Labels. That could get messy.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  10. #10
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: how to change color of label when mouse hover?

    Niya, quick question... do you have something installed that automatically turns any keywords to a different font color(Form, Label, etc.) or do you do that manually?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  11. #11

    Re: how to change color of label when mouse hover?

    Quote Originally Posted by Niya View Post
    Oh and one more thing I forgot to mention. Because in this case we're co-coordinating between two events, there is a need to preserve state information. By using an inherited control, we can encapsulate state information inside it. Would would really want to muddy up your Form code with extra private fields to deal with this Label states ? And then consider having multiple Labels. That could get messy.
    I only agree with what you say if the OP were to need this in multiple cases. If this is a one-off deal, then I think the effort is wasted in making a full blown custom control. It's not a ton of code (The UserControl) if the feature will be used repeatedly. The OP didn't state it was going to be for multiple labels (it only seems to be for one), but if it were to be for multiple labels, then yes, a UserControl is a much better way to go.

    EDIT: I would assume, dday, that he does it by hand. However, I have been proven wrong before

  12. #12
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: how to change color of label when mouse hover?

    Quote Originally Posted by dday9 View Post
    Niya, quick question... do you have something installed that automatically turns any keywords to a different font color(Form, Label, etc.) or do you do that manually?
    I do it manually. I do wish it was automatic though
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  13. #13
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: how to change color of label when mouse hover?

    Quote Originally Posted by formlesstree4 View Post
    I only agree with what you say if the OP were to need this in multiple cases. If this is a one-off deal, then I think the effort is wasted in making a full blown custom control...
    Better to have it and not need it than need it and not have it....I've been bitten by that snake enough times now to respect this
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  14. #14

    Re: how to change color of label when mouse hover?

    Quote Originally Posted by Niya View Post
    Better to have it and not need it than need it and not have it....I've been bitten by that snake enough times now to respect this
    Fair enough

  15. #15

    Thread Starter
    Registered User
    Join Date
    Mar 2013
    Posts
    9

    Re: how to change color of label when mouse hover?

    Thank you so much for your help guys.... And you are right formlesstree4...I only need the code for 2 labels..so i don't think i need to use inheritance. But thanks niya for the information.I definitely use that method in future..

    Thanks again!

  16. #16
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: how to change color of label when mouse hover?

    Quote Originally Posted by Niya View Post
    I do it manually. I do wish it was automatic though
    A vain hope. I actually thought you had been hit by advertising links, at first. That would be the problem with auto-coloring text in a post, too, only it would be even worse in effect. Sometimes we are talking about controls, sometimes we are just maintaining control. Figuring out which gets colored and which does not would almost certainly be wrong often enough to cause havoc. I think we'll just have to live with this one.
    My usual boring signature: Nothing

  17. #17
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: how to change color of label when mouse hover?

    Well I can live with doing it manually. Its just a personal style and you're right about software not being able to know when to apply colouring. Eg. If I mention the "Control" class I would colour it blue like: Control. But if I'm just talking about control, I'd leave it uncoloured. I don't think there is a heuristic algorithm in existence that can correctly determine 100% of the time, the right context of words written. Only humans can do that.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  18. #18
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: how to change color of label when mouse hover?

    Quote Originally Posted by Niya View Post
    Well I can live with doing it manually. Its just a personal style and you're right about software not being able to know when to apply colouring. Eg. If I mention the "Control" class I would colour it blue like: Control. But if I'm just talking about control, I'd leave it uncoloured. I don't think there is a heuristic algorithm in existence that can correctly determine 100% of the time, the right context of words written. Only humans can do that.
    Or you could create something, that just added the bb code around the keyword when you're typing in vbforums or any other site that would fit the context. Even if it's not in context, I think I'd rather delete the BB code than having to do it in manually.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  19. #19
    Junior Member
    Join Date
    Apr 2013
    Posts
    17

    Re: how to change color of label when mouse hover?

    Just change the color property of the button on the mousehover event of the mouse.

  20. #20
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: how to change color of label when mouse hover?

    Quote Originally Posted by dday9 View Post
    Or you could create something, that just added the bb code around the keyword when you're typing in vbforums or any other site that would fit the context. Even if it's not in context, I think I'd rather delete the BB code than having to do it in manually.
    You have a good point here since very few tokens in VB.Net are actual words in the English language. I may just write such an app. It would be an interesting little side project.

    Quote Originally Posted by annaharris View Post
    Just change the color property of the button on the mousehover event of the mouse.
    It was already established that the MouseHover event was unsuitable. MouseEnter and MouseLeave are better events to use.

    Btw...How in God's name did you get a red gem ?
    Last edited by Niya; May 24th, 2013 at 12:45 AM.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  21. #21
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: how to change color of label when mouse hover?

    Quote Originally Posted by Niya View Post
    Btw...How in God's name did you get a red gem ?
    By making numerous useless posts, most of which simply repeat something that someone else has already posted and some of which are wrong, and doing so one more than one forum, even when a person who is a moderator at two of those forums has asked you more than once not to do so and having that person explicitly disapprove of one of those posts.

  22. #22
    Hyperactive Member
    Join Date
    May 2012
    Posts
    304

    Re: how to change color of label when mouse hover?

    Quote Originally Posted by Niya View Post
    Why do people still trap events on a control's container to alter their behavior ? This isn't VB6, we have inheritance now:-
    vbnet Code:
    1. Public Class LabelEx
    2.     Inherits Label
    3.  
    4.     Private g_bHovering As Boolean
    5.  
    6.     Public Sub New()
    7.         Me.HoverColor = Color.Empty
    8.         g_bHovering = False
    9.     End Sub
    10.  
    11.     Public Property HoverColor As Color
    12.  
    13.     Protected Overrides Sub OnMouseEnter(e As System.EventArgs)
    14.         If Not Me.HoverColor = Color.Empty Then
    15.             SetHoverState(True)
    16.         End If
    17.  
    18.         MyBase.OnMouseEnter(e)
    19.     End Sub
    20.  
    21.     Protected Overrides Sub OnMouseLeave(e As System.EventArgs)
    22.         SetHoverState(False)
    23.  
    24.         MyBase.OnMouseLeave(e)
    25.     End Sub
    26.  
    27.     Protected Overrides Sub OnPaintBackground(pevent As System.Windows.Forms.PaintEventArgs)
    28.         If g_bHovering Then
    29.             pevent.Graphics.FillRectangle(New SolidBrush(Me.HoverColor), Me.ClientRectangle)
    30.         Else
    31.             MyBase.OnPaintBackground(pevent)
    32.         End If
    33.     End Sub
    34.  
    35.  
    36.     Private Sub SetHoverState(ByVal mouseHovering As Boolean)
    37.         If g_bHovering = mouseHovering Then Return
    38.         g_bHovering = mouseHovering
    39.         Me.Refresh()
    40.     End Sub
    41.  
    42.     Private Function ShouldSerializeHoverColor() As Boolean
    43.         Return Not Me.HoverColor = Color.Empty
    44.     End Function
    45.  
    46. End Class

    The above is an inherited Label that can change color when the mouse pointer hovers over it. All you have to do is spread it on a container(forms or whatever) and set its HoverColor property.



    Hi there, I stumbled across this post whilst working on a CustomLabel, that enables part of the Label to be bold. I'd now like the bold section to change colour on MouseEnter.

    I'm somewhat new to Custom Controls and Inheritance and have found them to play havoc with my designer (I'm probably missing something). I've been right clicking my project in solution explorer and adding a class, in the Class.vb file that is created I enter the code similar to below:

    vb Code:
    1. Public Class CustomLabel
    2.  
    3.     Inherits Label
    4.  
    5.     'Protected Overrides Sub OnMouseEnter(e As EventArgs)
    6.     '    Me.ForeColor = Color.Red
    7.     '    Me.Refresh()
    8.     '    MyBase.OnMouseEnter(e)
    9.     'End Sub
    10.  
    11.  
    12.     ' Allow bold font for right half of a label
    13.     ' indicated by the placement of a pipe char '|' in the string (ex. "Hello | World" will make bold 'World'
    14.     Protected Overrides Sub OnPaint(e As PaintEventArgs)
    15.         Dim drawPoint As Point = New Point(0, 0)
    16.         Dim boldDelimiter As Char = "|"c
    17.  
    18.         Dim ary() As String = Me.Text.Split(boldDelimiter)
    19.  
    20.         If ary.Length = 2 Then
    21.             Dim boldFont As Font = New Font("Segoe UI Semibold", "14")
    22.             Dim normalFont As Font = Me.Font
    23.  
    24.             ' Set TextFormatFlags to no padding so strings are drawn together.
    25.             Dim flags As TextFormatFlags = TextFormatFlags.NoPadding
    26.  
    27.             ' Declare a proposed size with dimensions set to the maximum integer value. [url]https://msdn.microsoft.com/en-us/library/8wafk2kt(v=vs.110).aspx[/url]
    28.             Dim proposedSize As Size = New Size(Integer.MaxValue, Integer.MaxValue)
    29.  
    30.             Dim boldSize As Size = TextRenderer.MeasureText(e.Graphics, ary(0), boldFont, proposedSize, flags)
    31.             Dim normalSize As Size = TextRenderer.MeasureText(e.Graphics, ary(1), normalFont, proposedSize, flags)
    32.  
    33.             Dim boldRect As Rectangle = New Rectangle(drawPoint, boldSize)
    34.             Dim normalRect As Rectangle = New Rectangle(boldRect.Right, boldRect.Top, normalSize.Width, normalSize.Height)
    35.  
    36.  
    37.  
    38.             TextRenderer.DrawText(e.Graphics, ary(0), boldFont, boldRect, Me.ForeColor, flags)
    39.             TextRenderer.DrawText(e.Graphics, ary(1), normalFont, normalRect, Me.ForeColor, flags)
    40.  
    41.         Else
    42.             ' Default to base class method
    43.             MyBase.OnPaint(e)
    44.         End If
    45.  
    46.     End Sub
    47. End Class

    I then rebuild the project and it appears as CustomLabel Components in the Toolbox and I drag it onto my form.

    This worked fine and I have the text I want in bold. Using your code above I started to add the OnMouseEnter overrrides, but as soon as I uncomment OnMouseEnter and drag the control onto my form again it fails to run.

    vb Code:
    1. 'Protected Overrides Sub OnMouseEnter(e As EventArgs)
    2.  
    3.     'End Sub

    Name:  Screenshot 2022-01-16 162304.jpg
Views: 1615
Size:  55.5 KB

    Am I using custom controls incorrectly, am I entering your code in the wrong place?

    Any help would be appreciated.

    Thank you.

  23. #23
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: how to change color of label when mouse hover?

    I just tested your code and it works fine. Try closing all tabs in Visual Studio and rebuilding the project. If that fails, try restarting Visual Studio.

    Also, what version of Visual Studio are you using?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  24. #24
    Hyperactive Member
    Join Date
    May 2012
    Posts
    304

    Re: how to change color of label when mouse hover?

    Niya, thanks for the response.

    Unfortunately neither worked, and now I get this all too familiar screen when I've been using custom controls :-(

    Attachment 183624

    I'm using Visual Studio Community 2019
    Version 16.9.4

  25. #25
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: how to change color of label when mouse hover?

    Well nothing is wrong your code. It works for me. Something else must be happening on your end. The error indicated it cannot find the CustomLabel class.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

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