Keep vertical sync between multiple RichTextBoxes

hey guys,

i have 6 side to side rtb's, im trying to keep the same vertical position for each of them. only the left most rtb actually has a visible scroll bar, the others do not. the idea is that as soon as the scroll changes in any of the rtb's, to set the same position in the rest.

so far my solution works...sort'of'ish. the problem im having is that when the scroll position changes, my rtb's freak out a little (the vertical position bounces around like crazy for about 500ms) and then end up at the right position eventually. i need a solution for this because for some reason during this little freak out session, my .SelectionStart property is getting set to 0 and causing problems elsewhere in my code.

    Private Sub rtbEditCol_VScroll(ByVal sender As Object, ByVal e As System.EventArgs)
            If bEditColXVScroll_Busy = False Then
                If CheckIfSameNoOfLinesExistInAllColumns() Then
                    bEditColXVScroll_Busy = True

                    Dim iDestVerPos As Integer = 0

                    rtbEditColVScroll = CType(sender, RichTextBox)
                    Dim iVerPos As Integer = GetScrollPos(rtbEditColVScroll.Handle, SB_VERT)

                    iVerPos <<= 16
                    Dim wParam As UInteger = CUInt(SB_THUMBPOSITION) Or CUInt(iVerPos)

                    For n As Int16 = INAME_COL1 To LAST_DATA_COL_INDEX
                        With CType(Me.TableLayoutPanel1.Controls.Item(ColRtbNamePrefix & n), RichTextBox)
                            iDestVerPos = GetScrollPos(.Handle, SB_VERT)
                            If iVerPos <> iDestVerPos And rtbEditColVScroll.Name <> .Name Then
                                If (SetScrollPos(.Handle, SB_VERT, iVerPos, True) <> -1) Then
                                    SendMessage(.Handle, WM_VSCROLL, New IntPtr(wParam), New IntPtr(0))
                                    My.Application.DoEvents()
                                    My.Application.DoEvents()
                                Else
                                    Debug.Print("Can't set info (Err: " & GetLastWin32Error() & ")")
                                End If
                            End If
                        End With
                    Next

                    bEditColXVScroll_Busy = False
                End If
            End If
      End Sub

Open in new window


from what i can debug, my problems seem to be getting caused within the DoEvents() method, however hopefully someone more wiser can point me in the right direction.

any help would be appreciated.

cheers
gem56Asked:
Who is Participating?
 
Wayne Taylor (webtubbs)Commented:
Take a look at this previously asked question...

"Lock Scrolling on Multiple RichTextBoxes"
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_24810186.html

Wayne
0
 
gem56Author Commented:
oh wow, thats perfect!

many thanks Wayne :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.