-
Jul 20th, 2006, 02:24 AM
#1
[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:
Dim currentLineIndex As Integer = myRTB.GetLineFromCharIndex(myRTB.SelectionStart) Dim lastLineIndex As Integer = myRTB.GetLineFromCharIndex(myRTB.TextLength) Dim currentColumnIndex As Integer = myRTB.SelectionStart - myRTB.GetFirstCharIndexFromLine(currentLineIndex) 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.
-
Jun 9th, 2008, 09:24 AM
#2
Fanatic Member
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.
-
Jun 9th, 2008, 08:44 PM
#3
Re: [2005] Getting RichTextBox Caret Coordinates
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.
-
Oct 10th, 2008, 09:14 AM
#4
Lively Member
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!
-
Oct 10th, 2008, 09:28 AM
#5
Re: [2005] Getting RichTextBox Caret Coordinates
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.
-
Oct 10th, 2008, 02:06 PM
#6
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|