RichTextBox Line at cursor question

M

Mike Edgewood

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.
 
S

Stoitcho Goutsev \(100\)

Mike,

RichEditBox supports methods:
GetCharIndexFromPosition and GetLineFromCharIndex.

Combining both you can get what you want:
GetLineFromCharIndex(GetCharIndexFromPosition(pos))

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.
 
M

Mike Edgewood

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
Then
Dim positionToSearch As Integer = _
rtbResults.GetCharIndexFromPosition(New Point(e.X,
e.Y))
Dim lineIndex As Integer = _
rtbResults.GetLineFromCharIndex(positionToSearch)
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
Next
End Sub
 
M

Mike Edgewood

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top