【问题标题】:Sync 2 list boxes to 1 scrollbar将 2 个列表框同步到 1 个滚动条
【发布时间】:2015-05-27 14:06:34
【问题描述】:

我有 2 个彼此相邻的列表框。一个持有订单,另一个持有订单的总成本。

出于显而易见的原因,我需要两个列表框同时滚动。

这是我尝试过的

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

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

任何帮助将不胜感激。

我正在使用 Visual Studio 2012,并且正在使用 vb 进行编码。

从我阅读的内容来看,_Scroll 已被删除。

我在想我可以删除订单列表框上的滚动条,并通过总计列表框上的滚动条来控制这两个框。

【问题讨论】:

  • 您可以使用DataGridView 并在列中显示数据——那么就不需要这种解决方法了。
  • 如果每个 ListBox 中的项目相互关联,最好将所有信息放在 ListView 或 DataGridView 中。这样它们将一起滚动,您不必担心同步它们。
  • @OneFineDay 我不允许使用表格,感谢您的回复,还有其他选择吗?
  • @Blackwood 我不允许使用数据库,所以我不认为我可以使用 DataGridView,我现在看看 Listview。
  • @Blackwood 您能否快速了解如何设置列表视图、添加和删除项目以及选择最后添加的值

标签: vb.net visual-studio-2012 scroll listbox listbox-control


【解决方案1】:

如果你想保持选定的索引同步,那么你可以这样做:

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真的很慢。

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 有更多项目,您将在运行时收到异常。

【讨论】:

  • 非常感谢,这非常有效:D。最初我使用这个 lstOrders.TopIndex = lstOrders.Items.Count - 1 lstTotalsEachOrder.TopIndex = lstTotalsEachOrder.Items.Count - 1,以保持最新的订单显示。但是谢谢我会同时使用这些
  • 顺便说一句,我使用了你的第二个选项 :)
  • 你不知道如何在一个列表框上隐藏滚动条?
  • @IeuanWalker - 试试ListBox.ScrollAlwaysVisible = False,但我担心你可能不会得到你期望的结果。
  • 不,我已经试过了:/,我想要一个经常隐藏。我现在搜索了很多网站,我认为不可能。
猜你喜欢
  • 1970-01-01
  • 2021-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多