[2005] Getting RichTextBox Caret Coordinates-VBForums
Results 1 to 6 of 6

Thread: [2005] Getting RichTextBox Caret Coordinates

  1. #1

    Thread Starter
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,601

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

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  2. #2
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    515

    Re: [2005] Getting RichTextBox Caret Coordinates

    Is there any way to use your method to highlight every line in richtextbox starting with say, "fox"

    This needs to happen as user is typing, so it has to be textchanged event.

    Appreciate any help.

  3. #3

    Thread Starter
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,601

    Re: [2005] Getting RichTextBox Caret Coordinates

    Quote Originally Posted by Xancholy
    Is there any way to use your method to highlight every line in richtextbox starting with say, "fox"

    This needs to happen as user is typing, so it has to be textchanged event.

    Appreciate any help.
    My code is about telling you where the caret is currently located. That's what it's for and that's what it does.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Lively Member
    Join Date
    Oct 2008
    Posts
    99

    Re: [2005] Getting RichTextBox Caret Coordinates

    I setup your code like the following:

    Code:
     Private Sub tb_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tb.TextChanged
            Dim currentLineIndex As Integer = tb.GetLineFromCharIndex(tb.SelectionStart)
            Dim lastLineIndex As Integer = tb.GetLineFromCharIndex(tb.TextLength)
            Dim currentColumnIndex As Integer = tb.SelectionStart - tb.GetFirstCharIndexFromLine(currentLineIndex)
            Label1.Text = String.Format("Line {0}/{1}, Column {2}", currentLineIndex + 1, lastLineIndex + 1, currentColumnIndex + 1)
        End Sub
    However as soon as I put text into the RTB i only get line1/2262. In other words the values stay static.

    Appreciate the help!

  5. #5

    Thread Starter
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,601

    Re: [2005] Getting RichTextBox Caret Coordinates

    Quote Originally Posted by dbansal
    I setup your code like the following:

    Code:
     Private Sub tb_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tb.TextChanged
            Dim currentLineIndex As Integer = tb.GetLineFromCharIndex(tb.SelectionStart)
            Dim lastLineIndex As Integer = tb.GetLineFromCharIndex(tb.TextLength)
            Dim currentColumnIndex As Integer = tb.SelectionStart - tb.GetFirstCharIndexFromLine(currentLineIndex)
            Label1.Text = String.Format("Line {0}/{1}, Column {2}", currentLineIndex + 1, lastLineIndex + 1, currentColumnIndex + 1)
        End Sub
    However as soon as I put text into the RTB i only get line1/2262. In other words the values stay static.

    Appreciate the help!
    That code does exactly what it's supposed to do: it updates the Label with the current caret location each time the Text changes. Of course, if you expect it to update the Label when you click somewhere in the control or move the caret with the arrow keys then you'll be disappointed because in those situations the Text doesn't change so that code won't be executed. If that's what you want then you'd have to handle the SelectionChanged event, which is raised whenever the SelectionStart or SelectionLength property changes. Just handling that will be enough as it covers situations where the Text changes too.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Lively Member
    Join Date
    Oct 2008
    Posts
    99

    Re: [2005] Getting RichTextBox Caret Coordinates

    Code:
       Private Sub tb_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tb.SelectionChanged
            Dim currentLineIndex As Integer = tb.GetLineFromCharIndex(tb.SelectionStart)
            Dim lastLineIndex As Integer = tb.GetLineFromCharIndex(tb.TextLength)
            Dim currentColumnIndex As Integer = tb.SelectionStart - tb.GetFirstCharIndexFromLine(currentLineIndex)
            Label1.Text = String.Format("Line {0}/{1}, Column {2}", currentLineIndex + 1, lastLineIndex + 1, currentColumnIndex + 1)
        End Sub
    That change in handle did the trick. thanks!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.