sync scrolling of 2 text areas


I have 2 text areas containing the same number of lines. I would like to use one scroll bar to scroll through both of them.

I tried to use TextAreaA.ScrollPosition=TextAreaB.ScrollPosition on the MouseWheel Event handler but that does not work as I expected.

Could anyone point me in the right direction? Is there a way to do this with a Vertical Scroll Bar?


this is the best I came up with:

  • disable vertical scroll bars on both text areas
  • do a line count in the text areas every time text is changed
  • set the maximum value of the vertical scroll bar to the highest of both maxima.

Now the sync scrolling works when I use the scroll bar, but the sync scrolling does not happen when I go up or down in one of the two areas.

Does someone know a better method?

Do they have to be TextAreas? Can they be Listboxes? I don’t think the TextArea will give you the events that you need.

Another option is to move the feature to a Timer that runs, say, every 100 ms that checks the position of the TextAreas and the Scrollbar. Whichever TextArea has focus would set the values of the other two controls, and the scrollbar would set it if neither have focus.

Following along Kem’s timer idea; something like this on the Timer Action event:

If TextArea1(0).ScrollPosition <> pLastPosition Then TextArea1(1).ScrollPosition = TextArea1(0).ScrollPosition pLastPosition = TextArea1(1).ScrollPosition ElseIf TextArea1(1).ScrollPosition <> pLastPosition Then TextArea1(0).ScrollPosition = TextArea1(1).ScrollPosition pLastPosition = TextArea1(0).ScrollPosition Else // do nothing End If

pLastPosition is a property to remember the last known position

What if neither match the last known position? :slight_smile:

I experimented several solutions, including setting one textarea with the other one scrollposition when it is modified. The result is choppy and erratic.

The very best result is to use one scroll bar that is set to the maximum ScrollPosition in the window Open event like so :

Sub Open() dim oldscroll as integer = TextArea1.ScrollPosition TextArea1.ScrollPosition = 10000 scrollmax = TextArea1.ScrollPosition TextArea1.ScrollPosition = oldscroll Scrollbar1.Maximum = scrollmax Scrollbar1.Value = oldscroll End Sub

Scrollmax is an integer window property

Both TextAreas vertical scrollbars have been disabled.

Then this in the scrollbar :

Sub ValueChanged() Textarea1.ScrollPosition = me.value Textarea2.ScrollPosition = me.value End Sub

Function MouseWheel(X As Integer, Y As Integer, deltaX as Integer, deltaY as Integer) As Boolean me.value = me.value-deltaY End Function

It is perfectly fluid and synchronous.

A bit late, but I only see these replies now.
Thank you all so much for all this input!