Display a tooltip when the mouse hover a specific word/phrase

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.


Answer this question

Display a tooltip when the mouse hover a specific word/phrase

  • erick_the_redd

    Thanks a lot... But if the label is multiline, then when I hover on the entire column of the word/phrase will cause a tooltip.. How can I make it so it will show the ToolTip only if it's in the word & line
    Thanks a lot (again),
    Ofir.

  • mykenk

    You're welcome.

    Please mark the appropriate post as "Answer" to close this thread.



  • The Samster

    Thanks A Lot!!!! It works!!
    Even though I had to change this line:
    Me.ToolTip1.Show(e.Link.LinkData.ToString, Me.LinkLabel1)
    To this:
    Me.ToolTip1.Show(e.Link.LinkData.ToString, Me.LinkLabel1, 2000)
    The reason is that without the interval set, the ToolTip was shown only for a very small moment.
    For your question, the user will know the word is sensitive as it's a language-teaching application, and the sensitive words aren't in English...
    THANK YOU VERY MUCH,
    Ofir.

  • 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

    Well, I checked it out, and it doesn't work so good.... I made it to display the mouseloc variable in another label, but it seems that it doesn't change after the mouse entered the label's area. Why is that And, I couldn't manage to make it work with multiline labels..
    Can you show me how to do it
    Thanks a lot,
    Ofir.

  • NoEgo

    by including the height of the sizeF variables. The height x 1 is the frist line, height x2 the second line, and so on. You'll compare this to the Y property of the mouse point.

  • ishaywei

    You didn't understand... I want the ToolTip to show Only if the mouse is over a specific word/phrase, not on every mousehover.
  • &amp;#23567;&amp;#26417;

    Sorry... I forgot.. :)
  • 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

    add a ToolTip control to the form where your label is in,just name it "ttTest",then write codes in your label's Hover event as follow:ttTest.show(your pre_defined text)
  • Siger

    Anyone
  • Display a tooltip when the mouse hover a specific word/phrase