Hello,
I have a label with a lot of text. I want to do, that when I hover specific words/phrases it will display a tooltip with a pre-defined text.
For example, if the label is written "Hello, welcome to my program. It will help you a lot.", then I want to do that if the user mousehover the word welcome it will display a tooltip with text of "Hi User", and if the mouse will hover the phrase "help you", it will display a tooltip with text of "In a lot of stuff".
Thanks,
Ofir.

Display a tooltip when the mouse hover a specific word/phrase
erick_the_redd
mykenk
You're welcome.
Please mark the appropriate post as "Answer" to close this thread.
The Samster
Henk Gijsbert
You'll have to measure the text so that you know the pixel locations of the words you want to make sensitive. Then you can check the mouse position to see of it is within the sensitive area and show the appropriate tooltip. Something like the following will work:
Dim mouseloc As Point
Private Sub Label2_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label2.MouseHover
Dim pt As Point = Me.mouseloc
Dim gfx As Graphics = Me.Label2.CreateGraphics
Dim sz1st1st As SizeF = gfx.MeasureString("Hello, ", Me.Label2.Font, Me.Label2.Width)
Dim sz1st2nd As SizeF = gfx.MeasureString("Hello, welcome", Me.Label2.Font, Me.Label2.Width)
Dim sz2nd1st As SizeF = gfx.MeasureString("Hello, welcome to my program. It will ", Me.Label2.Font, Me.Label2.Width)
Dim sz2nd2nd As SizeF = gfx.MeasureString("Hello, welcome to my program. It will help you", Me.Label2.Font, Me.Label2.Width)
gfx.Dispose()
If pt.X >= sz1st1st.Width And pt.X <= sz1st2nd.Width Then
Me.ToolTip1.Show("Hi User", Label2)
Else
Me.ToolTip1.Hide(Label2)
End If
If pt.X >= sz2nd1st.Width And pt.X <= sz2nd2nd.Width Then
Me.ToolTip1.Show("help you", Label2)
Else
Me.ToolTip1.Hide(Label2)
End If
End Sub
Private Sub Label2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label2.MouseMove
Me.mouseloc = e.Location
End Sub
donkaiser
NoEgo
ishaywei
&#23567;&#26417;
sandipan
Yeah, sorry, it will get much more complex for multiline labels. This is probably one of those times where what you want to do and what you should do are two different things.
First, how will the user even know that they can mouse over certain words Or which words are sensitive
If you use a control like a LinkLabel, it will be very easy to setup your tooltip text, regardless of label size, and the links will make it apparent as to what words are sensitive.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.LinkLabel1.Text = "Hello, welcome to my program. It will help you a lot."
Me.LinkLabel1.Links.Add(7, 7, "Hi User")
Me.LinkLabel1.Links.Add(38, 8, "help you")
End Sub
Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
Me.ToolTip1.Show(e.Link.LinkData.ToString, Me.LinkLabel1)
End Sub
Private Sub LinkLabel1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkLabel1.MouseLeave
Me.ToolTip1.Hide(Me.LinkLabel1)
End Sub
ESTAN
Siger