vb.netvisual-studio-2012scrolllistboxlistbox-control

Sync 2 list boxes to 1 scrollbar


I have 2 listboxes next to each other. One holds the order the other holds the total cost of the order.

For obvious reasons i need both listboxes to scroll simultaneously.

Here is what i have tried

Private Sub lstOrders_Scroll()
    lstTotalsEachOrder.TopIndex = lstOrders.TopIndex
End Sub

Private Sub lstTotalsEachOrder_Scroll()
    lstOrders.TopIndex = lstTotalsEachOrder.TopIndex
End Sub

Any help would be appreciated.

I am using Visual Studio 2012 and I'm coding in vb.

From what I read _Scroll has been removed.

I was thinking that I could remove the scrollbar on the order listbox and control the both boxes by the scroller on the totals listbox.


Solution

  • If you want to keep the selected indexes in sync, then you could do this:

    Option Strict On
    Option Explicit On
    
    Public Class Form1
    
        Private Sub ListBox_SelectedIndexChanged(sender As Object, e As EventArgs)
            Dim parentListBox As ListBox = DirectCast(sender, ListBox)
            Dim childListBox As ListBox = DirectCast(parentListBox.Tag, ListBox)
    
            If parentListBox.SelectedIndex < childListBox.Items.Count Then
                childListBox.SelectedIndex = parentListBox.SelectedIndex
            End If
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            Me.ListBox1.Tag = Me.ListBox2
            Me.ListBox2.Tag = Me.ListBox1
            AddHandler ListBox1.SelectedIndexChanged, AddressOf ListBox_SelectedIndexChanged
            AddHandler ListBox2.SelectedIndexChanged, AddressOf ListBox_SelectedIndexChanged
        End Sub
    
    End Class
    

    However to get the actual scrolling to sync up, you'd need to draw the listbox items yourself. The following accomplishes this task, but it's really slow to scroll the parent listbox.

    Option Strict On
    Option Explicit On
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            Me.ListBox1.DrawMode = DrawMode.OwnerDrawFixed
            Me.ListBox2.DrawMode = DrawMode.OwnerDrawFixed
            Me.ListBox1.Tag = Me.ListBox2
            Me.ListBox2.Tag = Me.ListBox1
            AddHandler Me.ListBox1.DrawItem, AddressOf ListBox_DrawItem
            AddHandler Me.ListBox2.DrawItem, AddressOf ListBox_DrawItem
        End Sub
    
        Private Sub ListBox_DrawItem(sender As Object, e As DrawItemEventArgs)
            Dim parentListBox As ListBox = DirectCast(sender, ListBox)
            Dim childListBox As ListBox = DirectCast(parentListBox.Tag, ListBox)
            e.DrawBackground()
            e.DrawFocusRectangle()
    
            Dim brsh As New SolidBrush(Color.Black)
    
            If String.Compare(e.State.ToString, DrawItemState.Selected.ToString) > 0 Then brsh.Color = Color.White
    
            e.Graphics.DrawString(CStr(parentListBox.Items(e.Index)), e.Font, brsh, New RectangleF(e.Bounds.Location, e.Bounds.Size))
    
            childListBox.TopIndex = parentListBox.TopIndex
    
        End Sub
    
    End Class
    

    Also take note that there is no error checking to make sure that the items can actually be scrolled to, so if one listbox has more items, you'll get an exception at runtime.