How can I sync the scrolling of two multiline textboxes in C# (WinForms)?
When you scroll up/down a line in TextBox A, TextBox B should scroll up/down too. The same the other way around.
Is this achievable without custom controls?
How can I sync the scrolling of two multiline textboxes in C# (WinForms)?
When you scroll up/down a line in TextBox A, TextBox B should scroll up/down too. The same the other way around.
Is this achievable without custom controls?
Hans Passant's solution worked like a charm but I needed a RichTextBox with both horizontal and vertical scrollbars. If you extend a RichTextBox instead of a TextBox you'll need to change the ScrollBars property accordingly (I used RichTextBoxScrollBars.Both).
If you want to sync horizontal scrolling as well, look for
(m.Msg == 0x115) || (m.Msg == 0x114)
.Here is what finally helped me to fix synchronization of multiple textboxes using mouse wheel.
I based it on very helpful Hans example.
Yes, you'll have to create a custom text box so you can detect it scrolling. The trick is to pass the scroll message to the other text box so it will scroll in sync. This really only works well when that other text box is about the same size and has the same number of lines.
Add a new class to your project and paste the code shown below. Compile. Drop two of the new controls from the top of the toolbox onto your form. Set the Buddy property to the other control on both. Run, type some text in both of them and watch them scroll in sync as you drag the scrollbar.
Hans Passant's solution was awesome. However I needed to sync three text boxes not just two.
So I modified it a little - but all credence should go to Hans, there's no way I would have even got close without his work - I thought I would post it back here in case others need the same.
SyncBox class:
Then in the form initilizer:
You can change this line:
to this:
and it will support scrolling with the mouse wheel as well.