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