Results 1 to 6 of 6

Thread: [2005] Getting RichTextBox Caret Coordinates

Threaded View

  1. #1

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    [2005] Getting RichTextBox Caret Coordinates

    C# version here.

    This method is more efficient than basically anything else you can come up with because under the hood it calls the SendMessage API, so there is no string manipulation to slow things down.
    VB Code:
    1. Dim currentLineIndex As Integer = myRTB.GetLineFromCharIndex(myRTB.SelectionStart)
    2. Dim lastLineIndex As Integer = myRTB.GetLineFromCharIndex(myRTB.TextLength)
    3. Dim currentColumnIndex As Integer = myRTB.SelectionStart - myRTB.GetFirstCharIndexFromLine(currentLineIndex)
    4.  
    5. Me.Label1.Text = String.Format("Line {0}/{1}, Column {2}", currentLineIndex + 1, lastLineIndex + 1, currentColumnIndex + 1)
    Put that in the SelectionChanged event handler of your RTB and it will show you your location without slowing your typing.
    Last edited by jmcilhinney; Oct 30th, 2008 at 11:24 PM.
    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

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