Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / February 2006

Tip: Looking for answers? Try searching our database.

RichTextBox Line at cursor question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mike Edgewood - 13 Feb 2006 16:57 GMT
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.
Stoitcho Goutsev (100) - 13 Feb 2006 19:28 GMT
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.

Signature

HTH
Stoitcho Goutsev (100)

> If a user clicks into a RichTextBox, how can I get that entire line
> where the click occured?
[quoted text clipped - 6 lines]
> So, how can I get the entire *line* the user clicked in a RichTextBox
> control.  Assume RTB has 100+ lines of RTF text.
Mike Edgewood - 13 Feb 2006 20:28 GMT
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.
Mike Edgewood - 13 Feb 2006 20:18 GMT
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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.