SplitContainer 中的顶部 ListView 不会添加垂直滚动条

Top ListView in SplitContainer won't add vertical scrollbar

我有一个 Winforms 应用程序,在 SplitContainer 中有两个 ListView。

当我拖动拆分器隐藏部分 Panel2 ListView 项目时,它会自动添加一个垂直滚动条。

当我拖动拆分器隐藏部分 Panel1 ListView 项目时,它没有添加垂直滚动条。

更改哪个 ListView 在哪个 Panel 中具有相同的行为。就好像 SplitContainer 或其面板的某些东西正在控制是否将垂直滚动条添加到 Panel1 中的 ListView 中。如何让位于顶部Panel1的ListView也自动添加垂直滚动条?

要复制,请使用一个表单创建一个简单的 Winforms 应用程序。这是我的表单代码,后面是设计器表单代码。

Public Class Form1
    Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        Timer1.Enabled = True
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Try
            Timer1.Enabled = False

            TechDateList.BeginUpdate()
            TechDateList.Items.Clear()
            StopsList.BeginUpdate()
            StopsList.Items.Clear()

            For i As Integer = 1 To 5
                Dim techItem = New ListViewItem
                techItem.UseItemStyleForSubItems = False
                techItem.SubItems(0).Text = Date.Now.ToString("MMM dd, yyyy")
                techItem.SubItems.Add(String.Format("Tech {0}", i))
                TechDateList.Items.Add(techItem)
            Next

            For i As Integer = 1 To 5
                Dim stopItem = New ListViewItem
                stopItem.UseItemStyleForSubItems = False
                stopItem.SubItems(0).Text = Choose(i, "AAA", "BBB", "CCC", "DDD", "EEE")
                stopItem.SubItems.Add(String.Format("Stop {0}", i))
                StopsList.Items.Add(stopItem)
            Next
        Catch ex As Exception
            MsgBox(ex.ToString(), MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Timer1_Tick Error 1")
        Finally
            TechDateList.EndUpdate()
            StopsList.EndUpdate()
        End Try

        Try
            ListSplitter.Panel1Collapsed = False
            ListSplitter.SplitterDistance = 125
            ListSplitter.SplitterWidth = 6
            TechDateList.Items.Item(0).Selected = True
        Catch ex As Exception
            MsgBox(ex.ToString(), MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Timer1_Tick Error 2")
        End Try
    End Sub
End Class
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container()
        Me.ListSplitter = New System.Windows.Forms.SplitContainer()
        Me.TechDateList = New System.Windows.Forms.ListView()
        Me.UInitial = CType(New System.Windows.Forms.ColumnHeader(), System.Windows.Forms.ColumnHeader)
        Me.SchedDate = CType(New System.Windows.Forms.ColumnHeader(), System.Windows.Forms.ColumnHeader)
        Me.StopsList = New System.Windows.Forms.ListView()
        Me.StopNum = CType(New System.Windows.Forms.ColumnHeader(), System.Windows.Forms.ColumnHeader)
        Me.StopName = CType(New System.Windows.Forms.ColumnHeader(), System.Windows.Forms.ColumnHeader)
        Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
        CType(Me.ListSplitter, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.ListSplitter.Panel1.SuspendLayout()
        Me.ListSplitter.Panel2.SuspendLayout()
        Me.ListSplitter.SuspendLayout()
        Me.SuspendLayout()
        '
        'ListSplitter
        '
        Me.ListSplitter.Dock = System.Windows.Forms.DockStyle.Fill
        Me.ListSplitter.FixedPanel = System.Windows.Forms.FixedPanel.Panel1
        Me.ListSplitter.Location = New System.Drawing.Point(0, 0)
        Me.ListSplitter.Name = "ListSplitter"
        Me.ListSplitter.Orientation = System.Windows.Forms.Orientation.Horizontal
        '
        'ListSplitter.Panel1
        '
        Me.ListSplitter.Panel1.Controls.Add(Me.TechDateList)
        Me.ListSplitter.Panel1Collapsed = True
        Me.ListSplitter.Panel1MinSize = 0
        '
        'ListSplitter.Panel2
        '
        Me.ListSplitter.Panel2.Controls.Add(Me.StopsList)
        Me.ListSplitter.Size = New System.Drawing.Size(384, 261)
        Me.ListSplitter.SplitterDistance = 25
        Me.ListSplitter.SplitterWidth = 1
        Me.ListSplitter.TabIndex = 1
        '
        'TechDateList
        '
        Me.TechDateList.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
            Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.TechDateList.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.UInitial, Me.SchedDate})
        Me.TechDateList.FullRowSelect = True
        Me.TechDateList.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None
        Me.TechDateList.HideSelection = False
        Me.TechDateList.LabelWrap = False
        Me.TechDateList.Location = New System.Drawing.Point(4, 0)
        Me.TechDateList.Margin = New System.Windows.Forms.Padding(0)
        Me.TechDateList.MultiSelect = False
        Me.TechDateList.Name = "TechDateList"
        Me.TechDateList.ShowGroups = False
        Me.TechDateList.Size = New System.Drawing.Size(258, 166)
        Me.TechDateList.TabIndex = 0
        Me.TechDateList.UseCompatibleStateImageBehavior = False
        Me.TechDateList.View = System.Windows.Forms.View.Details
        '
        'UInitial
        '
        Me.UInitial.Text = "Route"
        Me.UInitial.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
        Me.UInitial.Width = 100
        '
        'SchedDate
        '
        Me.SchedDate.Text = "Job Date"
        Me.SchedDate.Width = 133
        '
        'StopsList
        '
        Me.StopsList.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
            Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.StopsList.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.StopNum, Me.StopName})
        Me.StopsList.FullRowSelect = True
        Me.StopsList.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None
        Me.StopsList.HideSelection = False
        Me.StopsList.LabelWrap = False
        Me.StopsList.Location = New System.Drawing.Point(4, 0)
        Me.StopsList.Margin = New System.Windows.Forms.Padding(0)
        Me.StopsList.MultiSelect = False
        Me.StopsList.Name = "StopsList"
        Me.StopsList.ShowGroups = False
        Me.StopsList.Size = New System.Drawing.Size(258, 252)
        Me.StopsList.TabIndex = 0
        Me.StopsList.UseCompatibleStateImageBehavior = False
        Me.StopsList.View = System.Windows.Forms.View.Details
        '
        'StopNum
        '
        Me.StopNum.Text = "000"
        Me.StopNum.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
        Me.StopNum.Width = 34
        '
        'StopName
        '
        Me.StopName.Text = "Stop Name"
        Me.StopName.Width = 199
        '
        'Timer1
        '
        Me.Timer1.Interval = 250
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(384, 261)
        Me.Controls.Add(Me.ListSplitter)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ListSplitter.Panel1.ResumeLayout(False)
        Me.ListSplitter.Panel2.ResumeLayout(False)
        CType(Me.ListSplitter, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ListSplitter.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

    Friend WithEvents ListSplitter As SplitContainer
    Friend WithEvents TechDateList As ListView
    Friend WithEvents UInitial As ColumnHeader
    Friend WithEvents SchedDate As ColumnHeader
    Friend WithEvents StopsList As ListView
    Friend WithEvents StopNum As ColumnHeader
    Friend WithEvents StopName As ColumnHeader
    Friend WithEvents Timer1 As Timer
End Class

我猜测移动拆分器是在调整 Panel2 中的 ListView 的大小,而不是调整 Panel1 中的 ListView 的大小。我可能在某处遗漏了一些简单的东西。无论如何,如果我将这段代码添加到表单中,我会得到想要的结果:

    Private Sub ListSplitter_SplitterMoved(sender As Object, e As SplitterEventArgs) Handles ListSplitter.SplitterMoved
        TechDateList.Height = ListSplitter.Panel1.Height
    End Sub

根据我在 Designer 代码中看到的内容,TechDateList 的高度超过了 ListSplitter.Panel1 的高度:

Me.ListSplitter.SplitterDistance = 25

Me.TechDateList.Size = New System.Drawing.Size(258, 166)

确保 TechDateList 适合 Designer 中的 Panel1,例如

Me.ListSplitter.SplitterDistance = 125

Me.TechDateList.Size = New System.Drawing.Size(258, 120)

然后调整大小将按预期工作。

还考虑将 TechDateList 停靠到 Left - 列表将获得最大可能高度,并将随 Panel1

调整大小