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...
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.
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:
Public Class Form1
Dim scrollPosition As Integer = 0
Private Sub Panel1_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles Panel1.Scroll
If e.OldValue < e.NewValue Then
scrollPosition += 1
ElseIf e.OldValue > e.NewValue Then
scrollPosition -= 1
End If
If scrollPosition < 0 Then scrollPosition = 0
Panel1.AutoScrollPosition = TextBox1.GetPositionFromCharIndex(TextBox1.GetFirstCharIndexFromLine(scrollPosition))
End Sub
End Class
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.
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
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.