Results 1 to 3 of 3

Thread: Linked Textboxes - any action in one is automatically shared with the other

  1. #1

    Thread Starter
    Fanatic Member Peter Porter's Avatar
    Join Date
    Jul 2013
    Posts
    532

    Linked Textboxes - any action in one is automatically shared with the other

    The code links two textboxes, but it can be easily tweaked to handle more.

    If you highlight a selection of text in one TextBox, the other will do the same in sync. You would have to change HideSelection to False for both TextBoxes in their properties to see that.

    You can backspace the whole selection in both at the same time, or single characters by either the mouse or keyboard.

    Also pressing the Return key in one will create new lines in the other.

    Pasting is not working in this version, but I'll get it together tomorrow.

    The reason for linking the textboxes this way instead of simply TextBox1.text = Textbox2.text and visa-versa is because that simple approach will slow down or flicker the copying textbox if the length of the other textbox is too long. My code doesn't copy an entire textbox this way, but only where changes are made using SelectionStart and SelectionLength.

    Code:
    Public Class Form1
    
    
        Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
            ' Detects backspace key pressed within TextBox1
            If (e.KeyChar) = ControlChars.Back Then
                ' Checks TextBox1 selection length to see if more than one character was selected
                If TextBox1.SelectionLength > 1 Then
                    Try
                        ' TextBox2 backspaces the same number of text selected and backspaced in TextBox1
                        TextBox2.Focus()
                        SendKeys.Send("{DELETE}")
                    Catch ex As Exception
                    End Try
                Else
                    Try
                        ' TextBox2 backspaces single characters at the same position of the character backspaced in TextBox1
                        TextBox2.Select(TextBox1.SelectionStart - 1, TextBox1.SelectionLength + 1)
                        TextBox2.SelectedText = ""
                    Catch ex As Exception
                    End Try
                End If
            Else
                ' Detects if ESC key was pressed
                If (e.KeyChar) = Chr(27) Then
                    ' Nothing here to prevent ESC key character from
                    ' being entered into TextBoxes
                Else
                    ' Detects if Return key was pressed
                    If e.KeyChar = Chr(Keys.Return) Then
                        ' Creates new line in TextBox1 since
                        ' it must send focus to TextBox2 in order to
                        ' create a new line there by sending ENTER and
                        ' to set TextBox2's caret position
    
                        TextBox2.AppendText(Environment.NewLine + "")
                        TextBox2.Select(TextBox1.SelectionStart, TextBox1.SelectionLength)
    
                    Else
                        ' TextBox2 writes the same Text entered into TextBox1
                        Try
                            TextBox2.Select(TextBox1.SelectionStart, TextBox1.SelectionLength)
                            TextBox2.SelectedText = e.KeyChar
                        Catch ex As Exception
                            TextBox2.Select(TextBox1.SelectionStart, TextBox1.SelectionLength)
                            TextBox2.SelectedText = e.KeyChar
                        End Try
                    End If
                End If
            End If
        End Sub
    
    
        Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
            TextBox2.Select(TextBox1.SelectionStart, TextBox1.SelectionLength)
        End Sub
    
    
        Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
            TextBox2.Select(TextBox1.SelectionStart, TextBox1.SelectionLength)
        End Sub
    
    
        Private Sub TextBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseUp
            TextBox2.Select(TextBox1.SelectionStart, TextBox1.SelectionLength)
        End Sub
    
    
        Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
            ' Detects backspace key pressed within TextBox2
            If (e.KeyChar) = ControlChars.Back Then
                ' Checks TextBox2 selection length to see if more than one character was selected
                If TextBox2.SelectionLength > 1 Then
                    Try
                        ' TextBox1 backspaces the same number of text selected and backspaced in TextBox2
                        TextBox1.Focus()
                        SendKeys.Send("{DELETE}")
                    Catch ex As Exception
                    End Try
                Else
                    Try
                        ' TextBox1 backspaces single characters at the same position of the character backspaced in TextBox2
                        TextBox1.Select(TextBox2.SelectionStart - 1, TextBox2.SelectionLength + 1)
                        TextBox1.SelectedText = ""
                    Catch ex As Exception
                    End Try
                End If
            Else
                ' Detects if ESC key was pressed
                If (e.KeyChar) = Chr(27) Then
                    ' Nothing here to prevent ESC key character from
                    ' being entered into TextBoxes
                Else
                    ' Detects if Return key was pressed
                    If e.KeyChar = Chr(Keys.Return) Then
                        ' Creates new line in TextBox2 since
                        ' it must send focus to TextBox1 in order to
                        ' create a new line there by sending ENTER and
                        ' to set TextBox1's caret position
    
                        TextBox1.AppendText(Environment.NewLine + "")
                        TextBox1.Select(TextBox2.SelectionStart, TextBox2.SelectionLength)
    
                    Else
                        ' TextBox2 writes the same Text entered into TextBox1
                        Try
                            TextBox1.Select(TextBox2.SelectionStart, TextBox2.SelectionLength)
                            TextBox1.SelectedText = e.KeyChar
                        Catch ex As Exception
                            TextBox1.Select(TextBox2.SelectionStart, TextBox2.SelectionLength)
                            TextBox1.SelectedText = e.KeyChar
                        End Try
                    End If
                End If
            End If
    
        End Sub
    
    
        Private Sub TextBox2_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyUp
            TextBox1.Select(TextBox2.SelectionStart, TextBox2.SelectionLength)
        End Sub
    
    
        Private Sub TextBox2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox2.MouseDown
            TextBox1.Select(TextBox2.SelectionStart, TextBox2.SelectionLength)
        End Sub
    
    
        Private Sub TextBox2_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox2.MouseUp
            TextBox1.Select(TextBox2.SelectionStart, TextBox2.SelectionLength)
        End Sub
    
    
    End Class
    Last edited by Peter Porter; Nov 13th, 2019 at 02:56 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Linked Textboxes - any action in one is automatically shared with the other

    Your code ignores the fact that selection can be made using the keyboard as well as the mouse.

  3. #3

    Thread Starter
    Fanatic Member Peter Porter's Avatar
    Join Date
    Jul 2013
    Posts
    532

    Re: Linked Textboxes - any action in one is automatically shared with the other

    Oops, forgot about that!

    I've updated the code to include the keyboard for selecting text.
    Last edited by Peter Porter; Nov 13th, 2019 at 02:38 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width