将2个列表框同步到1个滚动条 [英] 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.
这是我尝试过的
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.
我正在使用Visual Studio 2012,并且正在vb中进行编码.
I am using Visual Studio 2012 and I'm coding in vb.
从我读到的_Scroll已删除.
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.
推荐答案
如果要使所选索引保持同步,则可以执行以下操作:
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
但是要使实际滚动同步起来,您需要自己绘制列表框项目.下面完成了此任务,但是滚动父listbox
确实很慢.
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
还请注意,没有错误检查以确保可以实际滚动到这些项目,因此,如果一个listbox
具有更多项目,则在运行时会出现异常.
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.
这篇关于将2个列表框同步到1个滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!