Synched RichTextBoxes, Numbered, 3 Jan 2018 - VB.NET
I've been playing with this all day, and I finally got it work perfectly. NickThissen's post on synchronizing richtextboxes got me started, but I slimmed it down, adding numbered lines.
There are no editing functions except for pasting text into it by pressing Ctrl-P besides typing.
RichTextBox1's backcolor is set to "Control" in properties, borderstyle "None", scrollbars "None", readonly "True", docked "Left", with a width of 70.
RichTextbox2's backcolor is "Window", borderstyle "None", wordwrap "False", docked "Fill".
Enjoy!
Code:
Public Class Form1
Const WM_USER As Integer = &H400
Const EM_GETSCROLLPOS As Integer = WM_USER + 221
Const EM_SETSCROLLPOS As Integer = WM_USER + 222
Declare Function SendMessage Lib "user32.dll" Alias "SendMessageW" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByRef lParam As Point) As Integer
Private Sub Form1_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
RichTextBox1.SelectionAlignment = HorizontalAlignment.Right
RichTextBox1.Text = "1"
RichTextBox1.Enabled = False
End Sub
Private Sub RichTextBox2_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox2.KeyUp
If (e.KeyCode = Keys.P AndAlso e.Modifiers = Keys.Control) Then
RichTextBox2.Text = Clipboard.GetText()
RichTextBox2.SelectAll()
RichTextBox2.SelectionFont = New Font("Microsoft Sans Serif", 10, FontStyle.Regular)
RichTextBox1.SelectAll()
RichTextBox1.SelectionFont = New Font("Microsoft Sans Serif", 10, FontStyle.Regular)
End If
End Sub
Private Sub RichTextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox2.TextChanged
Dim count As Integer
Dim count2 As String
Dim myList As List(Of String) = RichTextBox1.Lines.ToList()
Dim minus As Integer
Dim holder As String
count = RichTextBox2.Lines.Length
holder = CStr(RichTextBox1.Lines.Length)
count2 = " "
If RichTextBox1.Lines.Length > count Then
For minus = 1 To CInt(CDbl(holder) - count)
If myList.Count > count Then
myList.RemoveAt(myList.Count - 1)
RichTextBox1.Lines = myList.ToArray()
RichTextBox1.Refresh()
End If
Next
Else
If count = RichTextBox1.Lines.Length Then
count = RichTextBox1.Lines.Length
End If
End If
If count > RichTextBox1.Lines.Length + 1 Then
count2 = CStr(count - RichTextBox1.Lines.Length)
For minus = 1 To CInt(count2)
RichTextBox1.AppendText(Environment.NewLine & minus + count - CDbl(count2))
Next
End If
If RichTextBox1.Text.Length = 0 Then
RichTextBox1.Text = "1"
Else
End If
If RichTextBox1.Lines.Length < count Then
RichTextBox1.AppendText(Environment.NewLine & count)
Else
End If
If RichTextBox1.Text.Length = 2 Then
RichTextBox1.AppendText("2")
Else
End If
Dim pt2 As Point
SendMessage(RichTextBox2.Handle, EM_GETSCROLLPOS, 0, pt2)
SendMessage(RichTextBox1.Handle, EM_SETSCROLLPOS, 0, New Point(0, pt2.Y))
End Sub
Private Sub RichTextBox2_VScroll(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox2.VScroll
Dim pt2 As Point
SendMessage(RichTextBox2.Handle, EM_GETSCROLLPOS, 0, pt2)
SendMessage(RichTextBox1.Handle, EM_SETSCROLLPOS, 0, New Point(0, pt2.Y))
End Sub
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
Dim pt2 As Point
SendMessage(RichTextBox2.Handle, EM_GETSCROLLPOS, 0, pt2)
SendMessage(RichTextBox1.Handle, EM_SETSCROLLPOS, 0, New Point(0, pt2.Y))
End Sub
End Class
Coming feature...
Document map column, for fast navigation of large documents in a richtextbox, by hovering over an area of a captured thumbnail of it like in Notepad++.
The zipped project below uses an image divider separating the richtextboxes.
Last edited by Peter Porter; Jan 6th, 2018 at 07:34 PM.
Reason: Perfected, shortened, simplified code.