This project demonstrates how to track the current location of a richtextbox caret, record it's current location, with the option to return it to it's previously recorded location. It's not perfect, meaning if you record the caret position at say line 12, then you add characters to the length of line 9, your record position will drop the same amount of characters, probably sending it back to line 11 or lower.
What this idea could be used for is to write to 2 textboxes at the same time at the same caret location.
What's needed for below code:
* Panel docked top
* 2 Buttons inside it for recording and setting caret
* 3 Labels below buttons
* a GroupBox "Recorded Position" inside panel, docked right
* 2 Labels named RecLineLabel and RecColumnLabel inside GroupBox
* RichTextBox below panel, docked fill
Code:
Option Strict On
'This project tracks the current location of a RichTextBox caret, with the
'option to record it's location and return it to it's recorded location.
Public Class Form1
'for recording and setting caret position
Public start As Integer
Public length As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'display caret in RichTextBox
RichTextBox1.SelectionStart = start
RichTextBox1.[Select]()
'below gets caret's current line and column position
Dim line As Integer = RichTextBox1.GetLineFromCharIndex(RichTextBox1.SelectionStart)
Dim column As Integer = RichTextBox1.SelectionStart - RichTextBox1.GetFirstCharIndexFromLine(line)
Label2.Text = "Line " + CStr(CDbl(line.ToString) + 1)
Label3.Text = "Column " + CStr(CDbl(column.ToString) + 1)
line = Nothing : column = Nothing
End Sub
Private Sub RichTextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseDown
'below gets caret's current line and column position
Dim line As Integer = RichTextBox1.GetLineFromCharIndex(RichTextBox1.SelectionStart)
Dim column As Integer = RichTextBox1.SelectionStart - RichTextBox1.GetFirstCharIndexFromLine(line)
Label2.Text = "Line " + CStr(CDbl(line.ToString) + 1)
Label3.Text = "Column " + CStr(CDbl(column.ToString) + 1)
line = Nothing : column = Nothing
End Sub
Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
'below gets caret's current line and column position
Dim line As Integer = RichTextBox1.GetLineFromCharIndex(RichTextBox1.SelectionStart)
Dim column As Integer = RichTextBox1.SelectionStart - RichTextBox1.GetFirstCharIndexFromLine(line)
Label2.Text = "Line " + CStr(CDbl(line.ToString) + 1)
Label3.Text = "Column " + CStr(CDbl(column.ToString) + 1)
line = Nothing : column = Nothing
End Sub
'record caret position
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'for recording caret
start = RichTextBox1.SelectionStart
length = RichTextBox1.SelectionLength
'resetting caret so it stays visible after position is recorded
RichTextBox1.SelectionStart = start
RichTextBox1.SelectionLength = length
RichTextBox1.[Select]()
'to display the recorded caret position
Dim line As Integer = RichTextBox1.GetLineFromCharIndex(RichTextBox1.SelectionStart)
Dim column As Integer = RichTextBox1.SelectionStart - RichTextBox1.GetFirstCharIndexFromLine(line)
RecLineLabel.Text = "Line " + CStr(CDbl(line.ToString) + 1)
RecColumnLabel.Text = "Column " + CStr(CDbl(column.ToString) + 1)
line = Nothing : column = Nothing
End Sub
'no matter where the caret is in the RichTextBox, pressing this button will
'return it to it's recorded position
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'set caret to previously recorded position
RichTextBox1.SelectionStart = start
RichTextBox1.SelectionLength = length
RichTextBox1.[Select]()
'below gets caret's current line and column position
Dim line As Integer = RichTextBox1.GetLineFromCharIndex(RichTextBox1.SelectionStart)
Dim column As Integer = RichTextBox1.SelectionStart - RichTextBox1.GetFirstCharIndexFromLine(line)
Label2.Text = "Line " + CStr(CDbl(line.ToString) + 1) 'I know I could've simply wrote Label2.Text = RecLineLabel.Text
Label3.Text = "Column " + CStr(CDbl(column.ToString) + 1) 'it's just to show the tracked position matches recorded
line = Nothing : column = Nothing
End Sub
End Class
Created with Visual Basic 2010 Express
.Net Framework 4.0
Windows 7
Last edited by Peter Porter; May 7th, 2019 at 12:13 PM.