none
two textboxt control by one VScrollBar RRS feed

  • Question

  • Hi All

    I have 2 textboxts.I want VScrollBar1 can control both of textbox.

    How can I do if move the scorllbar to the end then both textboxt show the number 2616

    Thank

    Public Class Form1
        <Runtime.InteropServices.DllImport("user32.dll", EntryPoint:="SendMessageW")>
        Public Shared Function SendMessageW(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
        End Function
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            For I As Integer = 0 To 2616
                TextBox1.AppendText("Numbering =" & I & vbCrLf)
            Next
            For I As Integer = 0 To 2616
                TextBox2.AppendText("Numbering =" & I & vbCrLf)
            Next
    
            TextBox1.WordWrap = False
            TextBox2.WordWrap = False
    
        End Sub
    
        Private Sub VScrollBar1_Scroll(sender As Object, e As ScrollEventArgs) Handles VScrollBar1.Scroll
            VScrollBar1.Minimum = 0
            VScrollBar1.Maximum = 100
            VScrollBar1.Value = 20
    
    
            If e.OldValue > e.NewValue Then
                SendMessageW(TextBox1.Handle, &H115, &H0, 0)
                SendMessageW(TextBox2.Handle, &H115, &H0, 0)
            End If
            If e.OldValue < e.NewValue Then
                SendMessageW(TextBox1.Handle, &H115, &H1, 0)
                SendMessageW(TextBox2.Handle, &H115, &H1, 0)
            End If
        End Sub
    End Class
    

    Friday, November 1, 2019 1:52 PM

Answers

  • yup....okey.

    but in Form_load event error at

    TextBoxSync1.Buddy = TextBoxSync2
    TextBoxSync2.Buddy = TextBoxSync1

    Buddy is not member for TextBox

    thank.

    Buddy is a property of TextBoxSync so if you got this error you are most likely attempting to apply Buddy to a regular TextBox.

    Here is the proof in this code sample.

    https://1drv.ms/u/s!AtGAgKKpqdWjiQWlr1R3t6a47JvX?e=IsShcA


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by mipakteh Saturday, November 2, 2019 1:21 PM
    Saturday, November 2, 2019 12:43 PM
    Moderator

All replies

  • Perhaps an alternate solution? Use either ScrollBar.

    Add the following control to your project.

    Imports System.Runtime.InteropServices
    
    Public Class TextBoxSync
        Inherits TextBox
    
        Public Sub New()
            Multiline = True
            ScrollBars = ScrollBars.Vertical
        End Sub
        Public Property Buddy() As Control
    
        Private Shared _scrolling As Boolean
        Protected Overrides Sub WndProc(ByRef m As Message)
            MyBase.WndProc(m)
    
            If m.Msg = &H115 AndAlso Not _scrolling AndAlso Buddy IsNot Nothing AndAlso Buddy.IsHandleCreated Then
                _scrolling = True
                SendMessage(Buddy.Handle, m.Msg, m.WParam, m.LParam)
                _scrolling = False
            End If
    
        End Sub
        <DllImport("user32.dll", CharSet:=CharSet.Auto)>
        Private Shared Function SendMessage(hWnd As IntPtr, msg As Integer, wp As IntPtr, lp As IntPtr) As IntPtr
        End Function
    End Class
    
    

    Add two to the form

    Form code

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim lines = Enumerable.Range(0, 2000).
                    Select(Function(data) $"Numbering {data}").ToArray()
    
            ' Next two lines can be done in the property window for each TextBox
            TextBoxSync1.Buddy = TextBoxSync2
            TextBoxSync2.Buddy = TextBoxSync1
    
            TextBoxSync1.Lines = lines
            TextBoxSync2.Lines = lines
    
            TextBoxSync1.Select(0, 0)
    
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, November 1, 2019 3:34 PM
    Moderator
  • Thank you karen for your feedback.

    I copy your code and run but error at 

    TextBoxSync1.Buddy=TextboxSync2 

    TextBoxSync1 and TextBoxSync2 not declare.

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        <Runtime.InteropServices.DllImport("user32.dll", EntryPoint:="SendMessageW")>
        Public Shared Function SendMessageW(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
        End Function
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            For I As Integer = 0 To 2616
                TextBox1.AppendText("Numbering =" & I & vbCrLf)
            Next
            For I As Integer = 0 To 2616
                TextBox2.AppendText("Numbering =" & I & vbCrLf)
            Next
    
            TextBox1.WordWrap = False
            TextBox2.WordWrap = False
    
            Dim lines = Enumerable.Range(0, 2000).
                    Select(Function(data) $"Numbering {data}").ToArray()
    
    
    
            ' Next two lines can be done in the property window for each TextBox
            TextBoxSync1.Buddy = TextBoxSync2
            TextBoxSync2.Buddy = TextBoxSync1
    
            TextBoxSync1.Lines = lines
            TextBoxSync2.Lines = lines
    
            TextBoxSync1.Select(0, 0)
        End Sub
    
        Private Sub VScrollBar1_Scroll(sender As Object, e As ScrollEventArgs) Handles VScrollBar1.Scroll
            VScrollBar1.Minimum = 0
            VScrollBar1.Maximum = 100
            VScrollBar1.Value = 20
    
    
            If e.OldValue > e.NewValue Then
                SendMessageW(TextBox1.Handle, &H115, &H0, 0)
                SendMessageW(TextBox2.Handle, &H115, &H0, 0)
            End If
            If e.OldValue < e.NewValue Then
                SendMessageW(TextBox1.Handle, &H115, &H1, 0)
                SendMessageW(TextBox2.Handle, &H115, &H1, 0)
            End If
        End Sub
    
    End Class
    
    Public Class TextBoxSync
        Inherits TextBox
    
        Public Sub New()
            Multiline = True
            ScrollBars = ScrollBars.Vertical
        End Sub
        Public Property Buddy() As Control
    
        Private Shared _scrolling As Boolean
        Protected Overrides Sub WndProc(ByRef m As Message)
            MyBase.WndProc(m)
    
            If m.Msg = &H115 AndAlso Not _scrolling AndAlso Buddy IsNot Nothing AndAlso Buddy.IsHandleCreated Then
                _scrolling = True
                SendMessage(Buddy.Handle, m.Msg, m.WParam, m.LParam)
                _scrolling = False
            End If
    
        End Sub
        <DllImport("user32.dll", CharSet:=CharSet.Auto)>
        Private Shared Function SendMessage(hWnd As IntPtr, msg As Integer, wp As IntPtr, lp As IntPtr) As IntPtr
        End Function
    End Class
    
    
    

    I try like this but message show txt(0) is Nothing.

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        <Runtime.InteropServices.DllImport("user32.dll", EntryPoint:="SendMessageW")>
        Public Shared Function SendMessageW(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
        End Function
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim txt(1) As TextBoxSync
    
            For I As Integer = 0 To 2616
                TextBox1.AppendText("Numbering =" & I & vbCrLf)
            Next
            For I As Integer = 0 To 2616
                TextBox2.AppendText("Numbering =" & I & vbCrLf)
            Next
    
            TextBox1.WordWrap = False
            TextBox2.WordWrap = False
    
            Dim lines = Enumerable.Range(0, 2000).
                    Select(Function(data) $"Numbering {data}").ToArray()
    
    
    
            ' Next two lines can be done in the property window for each TextBox
            txt(0).Buddy = txt(1)
            txt(1).Buddy = txt(0)
    
            txt(1).Lines = lines
            txt(2).Lines = lines
    
            txt(1).Select(0, 0)
        End Sub
    
        Private Sub VScrollBar1_Scroll(sender As Object, e As ScrollEventArgs) Handles VScrollBar1.Scroll
            VScrollBar1.Minimum = 0
            VScrollBar1.Maximum = 100
            VScrollBar1.Value = 20
    
    
            If e.OldValue > e.NewValue Then
                SendMessageW(TextBox1.Handle, &H115, &H0, 0)
                SendMessageW(TextBox2.Handle, &H115, &H0, 0)
            End If
            If e.OldValue < e.NewValue Then
                SendMessageW(TextBox1.Handle, &H115, &H1, 0)
                SendMessageW(TextBox2.Handle, &H115, &H1, 0)
            End If
        End Sub
    
    End Class
    
    Public Class TextBoxSync
        Inherits TextBox
    
        Public Sub New()
            Multiline = True
            ScrollBars = ScrollBars.Vertical
        End Sub
        Public Property Buddy() As Control
    
        Private Shared _scrolling As Boolean
        Protected Overrides Sub WndProc(ByRef m As Message)
            MyBase.WndProc(m)
    
            If m.Msg = &H115 AndAlso Not _scrolling AndAlso Buddy IsNot Nothing AndAlso Buddy.IsHandleCreated Then
                _scrolling = True
                SendMessage(Buddy.Handle, m.Msg, m.WParam, m.LParam)
                _scrolling = False
            End If
    
        End Sub
        <DllImport("user32.dll", CharSet:=CharSet.Auto)>
        Private Shared Function SendMessage(hWnd As IntPtr, msg As Integer, wp As IntPtr, lp As IntPtr) As IntPtr
        End Function
    End Class
    
    
    

    Saturday, November 2, 2019 3:50 AM
  • You need to add the TextBox from the Toolbox or manually create them while there is no reason to manually create them unless you have a compelling reason too.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, November 2, 2019 10:55 AM
    Moderator
  • yup....okey.

    but in Form_load event error at

    TextBoxSync1.Buddy = TextBoxSync2
    TextBoxSync2.Buddy = TextBoxSync1

    Buddy is not member for TextBox

    thank.

    Saturday, November 2, 2019 12:32 PM
  • yup....okey.

    but in Form_load event error at

    TextBoxSync1.Buddy = TextBoxSync2
    TextBoxSync2.Buddy = TextBoxSync1

    Buddy is not member for TextBox

    thank.

    Buddy is a property of TextBoxSync so if you got this error you are most likely attempting to apply Buddy to a regular TextBox.

    Here is the proof in this code sample.

    https://1drv.ms/u/s!AtGAgKKpqdWjiQWlr1R3t6a47JvX?e=IsShcA


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by mipakteh Saturday, November 2, 2019 1:21 PM
    Saturday, November 2, 2019 12:43 PM
    Moderator
  • Good..working

    Thanl you very much Karen

    Saturday, November 2, 2019 1:21 PM