0

I have labels that I want to appear and disappear based on if the mouse is over it. I have a subroutine that's called when the mouse hovers over any label. It works when going from visible to not visible but not the other way around, and it also doesn't go back to the original state when the mouse is no longer on top of the label

Private Sub valueboxes_MouseHover(sender As Object, e As EventArgs)
    Dim thislabel As Label = sender

    thislabel.Visible = False
End Sub
  • It looks like [XY problem](https://xyproblem.info/). What's the actual requirement? When your label is invisible, it doesn't receive the mouse events. Maybe you are looking for [ToolTip](https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/tooltip-component-overview-windows-forms?view=netframeworkdesktop-4.8&WT.mc_id=DT-MVP-5003235) component or trying to simulate it for some reason? – Reza Aghaei Feb 03 '22 at 15:09
  • The Tooltip is what I'm looking for. I'm trying to display a value when the mouse hovers over part of a drawn line. Is there a way to make the tooltip show when the mouse is at, or near, a certain point? – 15hillada Feb 03 '22 at 15:20
  • You can use [ToolTip.Show(...)](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.tooltip.show?view=windowsdesktop-6.0&WT.mc_id=DT-MVP-5003235) method to show a tooltip, you just need to figure out when to call the method. "*When the mouse hovers over part of a drawn line*", then it looks like you have a drawing surface, and you may want to rely on Hover event of the drawing surface + a timer to show the tooltip. – Reza Aghaei Feb 03 '22 at 15:27
  • Also to do hit-testing for your line, you can use [GraphicsPath.IsOutlineVisible](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.drawing2d.graphicspath.isoutlinevisible?view=dotnet-plat-ext-6.0&WT.mc_id=DT-MVP-5003235) with a wide-enough pen. – Reza Aghaei Feb 03 '22 at 15:29
  • Or something like [Find if point lies on line segment](https://stackoverflow.com/q/7050186/7444103) if you're not using GraphicsPaths as containers to render your lines. – Jimi Feb 03 '22 at 15:30
  • 1
    "I have a subroutine that's called when the mouse hovers over any label. It works when going from visible to not visible but not the other way around" Right...because invisible controls don't fire events... – Idle_Mind Feb 03 '22 at 15:35
  • You need to handle the `MouseMove` event of the labels **Parent** container. Loop the labels collection to show a label when its `.Bounds.Contains(e.Location)` and hide the rest. As mentioned, can't use a ToolTip instead? – dr.null Feb 03 '22 at 16:50
  • @15hillada Take a look at this example: [Can I show ToolTip for the nodes I am painting on a panel?](https://stackoverflow.com/a/71148775/3110834) – Reza Aghaei Feb 16 '22 at 21:58

1 Answers1

0

Invert it instead of giving Visible property value as False

Private Sub label1_MouseHover(ByVal sender As Object, ByVal e As EventArgs)
    label2.Visible = Not label2.Visible
End Sub

Ref: How to toggle visibility in Windows Forms C#

Cheese
  • 1
  • 1
  • bro I asked how to turn it from invisible to visible not visible to not visible – 15hillada Feb 09 '22 at 10:09
  • the code toggle the visibility, when you hover the mouse first it will invisible and when you hover again it will be visible. – Cheese Feb 10 '22 at 04:44