|
-
Aug 24th, 2011, 08:31 AM
#1
Thread Starter
Addicted Member
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
-
Aug 24th, 2011, 08:42 AM
#2
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.
-
Aug 25th, 2011, 01:18 AM
#3
Thread Starter
Addicted Member
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
-
Aug 25th, 2011, 01:24 AM
#4
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.
-
Aug 25th, 2011, 03:32 AM
#5
Thread Starter
Addicted Member
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
-
Aug 25th, 2011, 03:50 AM
#6
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:
Public Class Form1 Private lineIndex As Integer = -1 Private Sub RichTextBox1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseMove 'Get the index of the line under the cursor. Dim cursorLocation = e.Location Dim charIndex = RichTextBox1.GetCharIndexFromPosition(cursorLocation) Dim newLineIndex = RichTextBox1.GetLineFromCharIndex(charIndex) 'Check whether the line index has changed. If Me.lineIndex <> newLineIndex Then 'Update the line index. Me.lineIndex = newLineIndex 'Get the line under the cursor. Dim line = RichTextBox1.Lines(Me.lineIndex) 'Update the ToolTip and show at the cursor location. ToolTip1.Show(line, RichTextBox1, cursorLocation, 5000) End If End Sub Private Sub RichTextBox1_TextChanged(sender As Object, e As System.EventArgs) Handles RichTextBox1.TextChanged 'Reset the line index to ensure that the new text will be shown if it's under the cursor. Me.lineIndex = -1 End Sub End Class
-
Aug 25th, 2011, 04:58 AM
#7
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|