Results 1 to 6 of 6

Thread: Vertical scrolling of two Textboxes Simultaneously

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    12

    Vertical scrolling of two Textboxes Simultaneously

    I have a text box with the names of student and another text box with their grades side by side in the same order..

    I want to synchronize the Vertical scrollbar of both the text box to a single scroll bar such that when i move it the contents in both the textbox moves simultaneously.

    I want the code to synchronize both the textbox with a single scroll bar...

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

    Re: Vertical scrolling of two Textboxes Simultaneously

    I'd suggest that TextBoxes are the wrong controls to be using. You should be using ListBoxes. You can then use the TopIndex properties to synchronise your controls. Even apart from that though, TextBoxes are designed for free-form text and ListBoxes are for lists. Always use the right tool for the job.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Vertical scrolling of two Textboxes Simultaneously

    Put 2 textboxes in a panel + set your panel's autosize property to true.
    Resize your textboxes so all lines are visible + set scrollbars to none. Then resize your panel so only part of your textboxes is visible. Then in your code:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim scrollPosition As Integer = 0
    4.  
    5.     Private Sub Panel1_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles Panel1.Scroll
    6.         If e.OldValue < e.NewValue Then
    7.             scrollPosition += 1
    8.         ElseIf e.OldValue > e.NewValue Then
    9.             scrollPosition -= 1
    10.         End If
    11.         If scrollPosition < 0 Then scrollPosition = 0
    12.         Panel1.AutoScrollPosition = TextBox1.GetPositionFromCharIndex(TextBox1.GetFirstCharIndexFromLine(scrollPosition))
    13.     End Sub
    14.  
    15. End Class

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Vertical scrolling of two Textboxes Simultaneously

    Or alternatively as JM said use the right control for the job. I'd use a listview. They're easy to use + you can show your data in orderly columns.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    12

    Re: Vertical scrolling of two Textboxes Simultaneously

    Thanks for ur help guys..

    I actually found a different way..

    I copied all the contents to richtextbox and then used the following code

    Code:
    Const WM_USER As Integer = &H400
    
        Const EM_GETSCROLLPOS As Integer = WM_USER + 221
    
        Const EM_SETSCROLLPOS As Integer = WM_USER + 222
    
        Declare Function SendMessage Lib "user32.dll" Alias "SendMessageW" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByRef lParam As Point) As Integer
    
        Private Sub RichTextBox1_VScroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.VScroll
    
            Dim pt As Point
    
            SendMessage(RichTextBox1.Handle, EM_GETSCROLLPOS, 0, pt)
    
            SendMessage(RichTextBox2.Handle, EM_SETSCROLLPOS, 0, pt)
    
        End Sub

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Vertical scrolling of two Textboxes Simultaneously

    I'd suggest that a RichTextBox is the wrong choice too. Each control has been designed for a specific purpose. There's a certain amount of leeway but each control generally works best for its intended purpose, otherwise it wouldn't have been created in the first place. What you use is up to you but, from what you've told us, the ListBox is the best control for the job.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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