Search code examples
winformslistviewsplitcontainer

Top ListView in SplitContainer won't add vertical scrollbar


I have a Winforms app with two ListViews in a SplitContainer.

When I drag the splitter to hide part of the Panel2 ListView items, it automatically adds a vertical scrollbar.
Vertical Scrollbar shows in bottom

When I drag the splitter to hide part of the Panel1 ListView items, it does not add a vertical scrollbar.
enter image description here

Changing which ListView is in which Panel has the same behavior. It's as if something about the SplitContainer or its panels is controlling whether the vertical scrollbar is added to the ListView in Panel1 or not. How to make whichever ListView is in the top Panel1 also automatically add the vertical scrollbar?

To replicate, create a simple Winforms application with one form. Here is my form code followed by the designer form code.

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

Solution

  • From what I see in Designer code, TechDateList Height exceeds ListSplitter.Panel1 Height:

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

    make sure that TechDateList fits Panel1 in Designer, e.g

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

    and then resize will work as expected.

    consider also docking TechDateList to Left - the list will get maximum possible Height and will resize with Panel1