-
May 21st, 2013, 10:56 PM
#1
Thread Starter
Registered User
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.
-
May 22nd, 2013, 08:47 AM
#2
Member
Re: how to change color of label when mouse hover?
Originally Posted by elodea
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)
-
May 22nd, 2013, 08:49 AM
#3
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!
-
May 22nd, 2013, 08:53 AM
#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.
-
May 22nd, 2013, 09:49 AM
#5
Re: how to change color of label when mouse hover?
Originally Posted by formlesstree4
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.
-
May 22nd, 2013, 01:18 PM
#6
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:
Public Class LabelEx
Inherits Label
Private g_bHovering As Boolean
Public Sub New()
Me.HoverColor = Color.Empty
g_bHovering = False
End Sub
Public Property HoverColor As Color
Protected Overrides Sub OnMouseEnter(e As System.EventArgs)
If Not Me.HoverColor = Color.Empty Then
SetHoverState(True)
End If
MyBase.OnMouseEnter(e)
End Sub
Protected Overrides Sub OnMouseLeave(e As System.EventArgs)
SetHoverState(False)
MyBase.OnMouseLeave(e)
End Sub
Protected Overrides Sub OnPaintBackground(pevent As System.Windows.Forms.PaintEventArgs)
If g_bHovering Then
pevent.Graphics.FillRectangle(New SolidBrush(Me.HoverColor), Me.ClientRectangle)
Else
MyBase.OnPaintBackground(pevent)
End If
End Sub
Private Sub SetHoverState(ByVal mouseHovering As Boolean)
If g_bHovering = mouseHovering Then Return
g_bHovering = mouseHovering
Me.Refresh()
End Sub
Private Function ShouldSerializeHoverColor() As Boolean
Return Not Me.HoverColor = Color.Empty
End Function
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.
-
May 22nd, 2013, 01:34 PM
#7
Re: how to change color of label when mouse hover?
Originally Posted by Niya
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).
-
May 22nd, 2013, 02:03 PM
#8
Re: how to change color of label when mouse hover?
Originally Posted by formlesstree4
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.
-
May 22nd, 2013, 02:13 PM
#9
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.
-
May 22nd, 2013, 02:16 PM
#10
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?
-
May 22nd, 2013, 02:17 PM
#11
Re: how to change color of label when mouse hover?
Originally Posted by Niya
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
-
May 22nd, 2013, 02:17 PM
#12
Re: how to change color of label when mouse hover?
Originally Posted by dday9
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
-
May 22nd, 2013, 02:25 PM
#13
Re: how to change color of label when mouse hover?
Originally Posted by formlesstree4
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
-
May 22nd, 2013, 02:27 PM
#14
Re: how to change color of label when mouse hover?
Originally Posted by Niya
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
-
May 23rd, 2013, 03:43 AM
#15
Thread Starter
Registered User
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!
-
May 23rd, 2013, 04:43 PM
#16
Re: how to change color of label when mouse hover?
Originally Posted by Niya
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
-
May 23rd, 2013, 05:18 PM
#17
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.
-
May 23rd, 2013, 05:27 PM
#18
Re: how to change color of label when mouse hover?
Originally Posted by Niya
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.
-
May 24th, 2013, 12:18 AM
#19
Junior Member
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.
-
May 24th, 2013, 12:42 AM
#20
Re: how to change color of label when mouse hover?
Originally Posted by dday9
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.
Originally Posted by annaharris
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.
-
May 24th, 2013, 02:29 AM
#21
Re: how to change color of label when mouse hover?
Originally Posted by Niya
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.
-
Jan 16th, 2022, 11:25 AM
#22
Hyperactive Member
Re: how to change color of label when mouse hover?
Originally Posted by Niya
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:
Public Class LabelEx
Inherits Label
Private g_bHovering As Boolean
Public Sub New()
Me.HoverColor = Color.Empty
g_bHovering = False
End Sub
Public Property HoverColor As Color
Protected Overrides Sub OnMouseEnter(e As System.EventArgs)
If Not Me.HoverColor = Color.Empty Then
SetHoverState(True)
End If
MyBase.OnMouseEnter(e)
End Sub
Protected Overrides Sub OnMouseLeave(e As System.EventArgs)
SetHoverState(False)
MyBase.OnMouseLeave(e)
End Sub
Protected Overrides Sub OnPaintBackground(pevent As System.Windows.Forms.PaintEventArgs)
If g_bHovering Then
pevent.Graphics.FillRectangle(New SolidBrush(Me.HoverColor), Me.ClientRectangle)
Else
MyBase.OnPaintBackground(pevent)
End If
End Sub
Private Sub SetHoverState(ByVal mouseHovering As Boolean)
If g_bHovering = mouseHovering Then Return
g_bHovering = mouseHovering
Me.Refresh()
End Sub
Private Function ShouldSerializeHoverColor() As Boolean
Return Not Me.HoverColor = Color.Empty
End Function
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:
Public Class CustomLabel
Inherits Label
'Protected Overrides Sub OnMouseEnter(e As EventArgs)
' Me.ForeColor = Color.Red
' Me.Refresh()
' MyBase.OnMouseEnter(e)
'End Sub
' Allow bold font for right half of a label
' indicated by the placement of a pipe char '|' in the string (ex. "Hello | World" will make bold 'World'
Protected Overrides Sub OnPaint(e As PaintEventArgs)
Dim drawPoint As Point = New Point(0, 0)
Dim boldDelimiter As Char = "|"c
Dim ary() As String = Me.Text.Split(boldDelimiter)
If ary.Length = 2 Then
Dim boldFont As Font = New Font("Segoe UI Semibold", "14")
Dim normalFont As Font = Me.Font
' Set TextFormatFlags to no padding so strings are drawn together.
Dim flags As TextFormatFlags = TextFormatFlags.NoPadding
' 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]
Dim proposedSize As Size = New Size(Integer.MaxValue, Integer.MaxValue)
Dim boldSize As Size = TextRenderer.MeasureText(e.Graphics, ary(0), boldFont, proposedSize, flags)
Dim normalSize As Size = TextRenderer.MeasureText(e.Graphics, ary(1), normalFont, proposedSize, flags)
Dim boldRect As Rectangle = New Rectangle(drawPoint, boldSize)
Dim normalRect As Rectangle = New Rectangle(boldRect.Right, boldRect.Top, normalSize.Width, normalSize.Height)
TextRenderer.DrawText(e.Graphics, ary(0), boldFont, boldRect, Me.ForeColor, flags)
TextRenderer.DrawText(e.Graphics, ary(1), normalFont, normalRect, Me.ForeColor, flags)
Else
' Default to base class method
MyBase.OnPaint(e)
End If
End Sub
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:
'Protected Overrides Sub OnMouseEnter(e As EventArgs)
'End Sub
Am I using custom controls incorrectly, am I entering your code in the wrong place?
Any help would be appreciated.
Thank you.
-
Jan 16th, 2022, 06:04 PM
#23
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?
-
Jan 17th, 2022, 01:45 PM
#24
Hyperactive Member
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
-
Jan 17th, 2022, 08:24 PM
#25
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|