RichTextBox Line at cursor question

RichTextBox Line at cursor question

Post by Mike Edgew » Wed, 15 Feb 2006 01:57:06

If a user clicks into a RichTextBox, how can I get that entire line
where the click occured?

Goal: If *line* is a path\filename, I want to open it in my own
personal display viewer. After pulling hair trying, unsuccessfully, to
get "file:" and DetectURL to do what I want, I decided to write my own.
(file: does not work if the name or path has a space)

So, how can I get the entire *line* the user clicked in a RichTextBox
control. Assume RTB has 100+ lines of RTF text.

RichTextBox Line at cursor question

Post by 10 » Wed, 15 Feb 2006 04:28:59


RichEditBox supports methods:
GetCharIndexFromPosition and GetLineFromCharIndex.

Combining both you can get what you want:

Be advised though that the line index is the physical line on the scree. If
one logical line is wrapped over more than one physical line this method
will return different line indexes depending where you click on the line.
From my experience this is almost never what one expects. Unfortunately the
only way to get the logical line index (without writing any code) is to turn
off the text wrapping.
This is not only missing functionality in the .NET control it cannot be done
with win API either.

Stoitcho Goutsev (100)


RichTextBox Line at cursor question

Post by Mike Edgew » Wed, 15 Feb 2006 05:18:34

I think I got it.

Private Sub rtbResults_MouseDown(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles rtbResults.MouseDown
If e.Clicks = 1 And e.Button = Windows.Forms.MouseButtons.Left
Dim positionToSearch As Integer = _
rtbResults.GetCharIndexFromPosition(New Point(e.X,
Dim lineIndex As Integer = _
Dim lineString As String = rtbResults.Lines(lineIndex)
' do whatever
End If
End Sub

My goal here was to highlight filenames and display them as links and
react upon them when clicked. Although the RichTextBox will create a
link for files, it stops at the first space it encounters, rendering it
absolutely usless for the task.

This is what I use to highlight the files in the rtf box. If any
flaws, please update and post a reply.

Private Sub HighlightDirs(ByVal rtb As RichTextBox)
Dim r As Regex = New Regex( _
"^([a-zA-Z]\:|\\\\[^\/\\:*?""<>|]+\\[^\/\\:*?""<>|]+)" & _
"(\\[^\/\\:*?""<>|]+)+(\.[^\/\\:*?""<>|]+)$" _
, RegexOptions.IgnoreCase Or RegexOptions.Multiline _
Or RegexOptions.Compiled)
For Each m As Match In r.Matches(rtb.Text)
rtb.Select(m.Index, m.Length)
rtb.SelectionFont = New Font(Me.rtbResults.Font, _
FontStyle.Bold Or FontStyle.Underline)
rtb.SelectionColor = Color.Blue
End Sub

RichTextBox Line at cursor question

Post by Mike Edgew » Wed, 15 Feb 2006 05:28:55

Yeah, I saw that in the help description. It would make for a lot of
extra "checking" in your code to determine the actual line index so it
corresponded to the collection of lines. Seems flawed. But then
again, so does their method of autoincrementing version numbers using
the number of seconds from midnight rather than incrementing a given
number. But don't get me started on that one. That is at the top of
my list of VS head scratchers.