Results 1 to 7 of 7

Thread: How to get text when hovering in RichTextbox ?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    128

    How to get text when hovering in RichTextbox ?

    Hello

    I'm using Richtextbox in VB2008 winforms.

    I'd like to know if that's possible to locate the text that I'm currently positioned on, in order to show it on a tooltip.
    I've just googled but found nothing. It seems like I have to use MouseHover event, but can't find how to find the position of where I'm currently on.

    Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to get text when hovering in RichTextbox ?

    You can use the Cursor.Position property to get the mouse cursor location and then use the PointToClient method of the RTB to get that relative to the control. The RTB then has various methods that work with positions, like GetCharIndexFromPosition or something like that.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    128

    Re: How to get text when hovering in RichTextbox ?

    Thank you! It works!

    Actually it works on debug time. I don't know why it does not work on real time - do you know the reason?
    Please find the code

    Code:
    Private Sub RichTextBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseMove
            Dim p, p2 As Point
            ToolTip1.RemoveAll()
            p = Cursor.Position
            p2 = rtb.PointToClient(p)
    
            Dim i As Integer = rtb.GetCharIndexFromPosition(p2)
           
            Dim lines() As String = Split(rtb.Text, vbLf)   
    
            Dim str As String = lines(rtb.GetLineFromCharIndex(i))
            Dim str_pos As Integer = str.IndexOf("|", 0)
            Dim str2 As String = "Version: " & str.Substring(0, str_pos - 1).Trim
         
            ' str2 is never empty
            ToolTip1.Show(str2, Me.rtb, 5000)
    
    
    End Sub

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to get text when hovering in RichTextbox ?

    I don't know why it does not work on real time - do you know the reason?
    It might help to know exactly what "does not work" means. Is an exception thrown? If so then that should be a good place to start the diagnosis. If not then you may have to add some tracing code.
    Code:
    Dim lines() As String = Split(rtb.Text, vbLf)
    Presumably you are not aware that the RichTextBox has a Lines property.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    128

    Re: How to get text when hovering in RichTextbox ?

    Sorry.
    What I meant is that the tooltip is not shown on run-time (shown in debug mode only). There are no excepetions.

    Not included in provided code, but I checked it with writing to log files right after the tooltip should be shown, and it writes hundreds of messages... so it seems to be that the application arrives tooltip.show line, but it is not displayed.

    Thanks again

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to get text when hovering in RichTextbox ?

    My guess would be that the ToolTip is being displayed in a position that you can't see it. I notice that you aren't specifying the location at all in that code.

    You said originally that you were going to use the MouseHover event. That event doesn't provide the cursor location so you need to determine it as a I suggested. If you're going to use the MouseMove event then you don't have to calculate it because the event provides it for you.

    If you are going to use the MouseMove event then I'd suggest only updating the ToolTip when the line number changes, which will reduce the amount of processing you have to do.
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private lineIndex As Integer = -1
    4.  
    5.     Private Sub RichTextBox1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseMove
    6.         'Get the index of the line under the cursor.
    7.         Dim cursorLocation = e.Location
    8.         Dim charIndex = RichTextBox1.GetCharIndexFromPosition(cursorLocation)
    9.         Dim newLineIndex = RichTextBox1.GetLineFromCharIndex(charIndex)
    10.  
    11.         'Check whether the line index has changed.
    12.         If Me.lineIndex <> newLineIndex Then
    13.             'Update the line index.
    14.             Me.lineIndex = newLineIndex
    15.  
    16.             'Get the line under the cursor.
    17.             Dim line = RichTextBox1.Lines(Me.lineIndex)
    18.  
    19.             'Update the ToolTip and show at the cursor location.
    20.             ToolTip1.Show(line, RichTextBox1, cursorLocation, 5000)
    21.         End If
    22.     End Sub
    23.  
    24.     Private Sub RichTextBox1_TextChanged(sender As Object, e As System.EventArgs) Handles RichTextBox1.TextChanged
    25.         'Reset the line index to ensure that the new text will be shown if it's under the cursor.
    26.         Me.lineIndex = -1
    27.     End Sub
    28.  
    29. End Class
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    128

    Re: How to get text when hovering in RichTextbox ?

    Thank you very much! Exactly what I needed

Tags for this Thread

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