synchronize textboxes

shreyas123

Member
Joined
Jan 7, 2007
Messages
9
Programming Experience
Beginner
I have two text boxes on the same form one contains my original data and other displays the formatted data. Each line has the same meaning so when I scroll down my first text box I want to scroll down another text automatically How can I do this
I even tried adding vscrollbar but I dont know how I write the code to move up and down the text in the text box.
Please suggest something
Thank You
 
Automating "moving up and down" in textbox is done by getting/analyzing char/line indexes, making selection to place caret, then using the whimsical ScrollToCaret method. Please review all the TextBox class members, you should find many useful ones.

I have coded something close to what you ask, it selects the full line in other textbox by same line index as the current line in first textbox. Be sure to set the HideSelection property in other textbox to False in order to see the selection since other textbox is not focused.

The problem with getting absolute visible line-by-lines sync is that the ScrollToCaret method does not care as long as the caret is visible anywhere in textbox. You can get index of first visible line, but in different situations will have to first scroll to first line or last line then to "first visible line". This could flicker a lot, but you can use the SuspendLayout and ResumeLayout for suppressing UI update while this happens. Maybe you could figure this out.

In the mean time test this code:
VB.NET:
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles TextBox1.KeyUp
    syncOtherTextbox(TextBox1, TextBox2)
End Sub
 
Private Sub TextBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles TextBox1.MouseUp
    syncOtherTextbox(TextBox1, TextBox2)
End Sub
 
Sub syncOtherTextbox(ByVal thisTextBox As TextBox, ByVal otherTextBox As TextBox)
    otherTextBox.Focus()
    Dim selectedLine As Integer = thisTextBox.GetLineFromCharIndex(thisTextBox.SelectionStart)
    otherTextBox.Select(otherTextBox.GetFirstCharIndexFromLine(selectedLine), otherTextBox.Lines(selectedLine).Length)
    otherTextBox.ScrollToCaret()
    thisTextBox.Focus()
End Sub
 
Here is the rework to get absolute sync:
VB.NET:
Sub syncOtherTextbox(ByVal thisTextBox As TextBox, ByVal otherTextBox As TextBox)
    otherTextBox.Focus()
    [COLOR=darkgreen]'scroll to last line[/COLOR]
    Dim lastLine As Integer = thisTextBox.Lines.Length - 1
    otherTextBox.Select(otherTextBox.GetFirstCharIndexFromLine(lastLine), 0)
    otherTextBox.ScrollToCaret()
    [COLOR=darkgreen]'scroll back to first visible line[/COLOR]
    Dim topleft As New Point(thisTextBox.ClientRectangle.Left + 3, thisTextBox.ClientRectangle.Top + 3)
    Dim firstCharIndex As Integer = thisTextBox.GetCharIndexFromPosition(topleft)
    Dim firstLine As Integer = thisTextBox.GetLineFromCharIndex(firstCharIndex)
    otherTextBox.Select(otherTextBox.GetFirstCharIndexFromLine(firstLine), 0)
    otherTextBox.ScrollToCaret()
    [COLOR=darkgreen]'mark current line[/COLOR]
    Dim selectedLine As Integer = thisTextBox.GetLineFromCharIndex(thisTextBox.SelectionStart)
    otherTextBox.Select(otherTextBox.GetFirstCharIndexFromLine(selectedLine), otherTextBox.Lines(selectedLine).Length)
    [COLOR=darkgreen]'return[/COLOR]
    thisTextBox.Focus()
End Sub
 
Back
Top