0

When I resize my form with the resize handle on the bottom right corner of the form, it fires the sizechanged event of my user control. Inside the user control I have code to set the maximum and value of the horizontal scroll bar.

If I resize to the right, the code works as expected. When I resize to the left, the scrolling thumb gets really big and scrolling does not work as expected. It's as if the Maximum got set to a low number, but my Debug.WriteLine shows that this is not the case. Actually, as I slowly resize the form to be narrower, it quickly toggles between doing it right and doing it wrong.

I deal with scroll bars rarely and when I do they are always a pain. Is there a ScrollBar guru who knows why this is happening? I googled it and searched SO too, but I really don't know what to search on.

Here is my code. The relevant part is what is called from the sizechanged event handler, which is almost to the bottom of the code.

Imports System.Reflection

Public Class Grid


  Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or ControlStyles.DoubleBuffer, True) 'Or ControlStyles.ResizeRedraw 
  End Sub


  Private _FirstVisibleRow As Row
  Private Property FirstVisibleRow As Row
    Get
      If _FirstVisibleRow Is Nothing And Rows.Any Then
        _FirstVisibleRow = Rows.First
      End If
      Return _FirstVisibleRow
    End Get
    Set(value As Row)
      _FirstVisibleRow = value
      LastVisibleRow = GetLastVisibleRow()
    End Set
  End Property


  Private _HeaderRow As Row
  Public Property HeaderRow As Row
    Get
      If _HeaderRow Is Nothing Then
        _HeaderRow = New Row(Me, RowHeight)
        For Each Column As Column In Columns
          _HeaderRow.Cells.Add(New Cell(Column, _HeaderRow))
        Next
      End If
      Return _HeaderRow
    End Get
    Private Set(value As Row)
      _HeaderRow = value
    End Set
  End Property

  Private _LastVisibleRow As Row
  Private Property LastVisibleRow As Row
    Get
      If _LastVisibleRow Is Nothing Then
        _LastVisibleRow = GetLastVisibleRow()
      End If
      Return _LastVisibleRow
    End Get
    Set(value As Row)
      _LastVisibleRow = value
    End Set
  End Property


  Private _TotalColumnWidth As Integer
  Friend Property TotalColumnWidth As Integer
    Get
      If _TotalColumnWidth = Nothing Then
        _TotalColumnWidth = GetTotalColumnWidth()
      End If
      Return _TotalColumnWidth
    End Get
    Set(value As Integer)
      _TotalColumnWidth = value
      SetScrollBarVisibility()
    End Set
  End Property

  Private _TotalRowHeight As Integer
  Friend Property TotalRowHeight As Integer
    Get
      If _TotalRowHeight = Nothing Then
        _TotalRowHeight = GetTotalRowHeight()
      End If
      Return _TotalRowHeight
    End Get
    Set(value As Integer)
      _TotalRowHeight = value
      SetScrollBarVisibility()
    End Set
  End Property

  Private _VisibleGridSize As Size
  Private Property VisibleGridSize As Size
    Get
      If _VisibleGridSize = Nothing Then
        _VisibleGridSize = GetVisibleGridSize()
      End If
      Return _VisibleGridSize
    End Get
    Set(value As Size)
      _VisibleGridSize = value
      VisibleRowCount = GetVisibleRowCount()
      SetScrollBarVisibility()
    End Set
  End Property

  Private Sub SetScrollBarVisibility()
    VScrollBar1.Bounds = New Rectangle(Width - VScrollBar1.Width, 0, VScrollBar1.Width, Height - IIf(HScrollBar1.Visible, HScrollBar1.Height, 0))
    HScrollBar1.Bounds = New Rectangle(0, Height - HScrollBar1.Height, Width - IIf(VScrollBar1.Visible, VScrollBar1.Width, 0), HScrollBar1.Height)
    VScrollBar1.Maximum = Math.Max(0, TotalRowHeight - Height - IIf(HScrollBar1.Visible, HScrollBar1.Height, 0))
    HScrollBar1.Maximum = Math.Max(0, TotalColumnWidth - Width + IIf(VScrollBar1.Visible, VScrollBar1.Width, 0))
    HScrollBar1.Value = 0
    VScrollBar1.Visible = TotalRowHeight > VisibleGridSize.Height
    HScrollBar1.Visible = TotalColumnWidth > VisibleGridSize.Width
    Debug.WriteLine(String.Format("HScrollBar1.Minimum {0}, HScrollBar1.Maximum {1}, HScrollBar1.Value {2}", HScrollBar1.Minimum, HScrollBar1.Maximum, HScrollBar1.Value))
  End Sub

  Private _VisibleRowCount As Integer
  Private Property VisibleRowCount As Integer
    Get
      If _VisibleRowCount = 0 Then
        _VisibleRowCount = GetVisibleRowCount()
      End If
      Return _VisibleRowCount
    End Get
    Set(value As Integer)
      _VisibleRowCount = value
      LastVisibleRow = GetLastVisibleRow()
      PageHeight = GetPageHeight()
    End Set
  End Property

  Private Function GetLastVisibleRow() As Row
    If Not Rows.Any Then Return Nothing
    Return Rows(Math.Min(FirstVisibleRow.Index + VisibleRowCount - 1, Rows.Count - 1))
  End Function

  Private Function GetPageHeight() As Integer
    Return RowHeight * GetVisibleRowCount()
  End Function

  Private Function GetRowHeight() As Integer
    Return TextRenderer.MeasureText("X", Font).Height + 6
  End Function

  Private Function GetVisibleGridSize() As Size
    Return New Size(Width - IIf(VScrollBar1.Visible, VScrollBar1.Width, 0),
                    Height - HeaderRow.Height - IIf(HScrollBar1.Visible, HScrollBar1.Height, 0)) 'don't count header row or horiz scroll bar
  End Function

  Private Function GetVisibleRowCount() As Integer
    Return Math.Ceiling(VisibleGridSize.Height / RowHeight)
  End Function


  Public Shadows Sub Refresh()
    ClearSelection()
    _HeaderRow = Nothing
    _Rows = Nothing
    AutoSizeColumns()

    TotalRowHeight = GetTotalRowHeight()

    Invalidate()
  End Sub

  Friend Function GetTotalColumnWidth() As Integer
    Return Columns.Select(Function(x) x.Width).Aggregate(0, Function(x, y) x + y)
  End Function

  Friend Function GetTotalRowHeight() As Integer
    Return Rows.Select(Function(x) x.Height).Aggregate(0, Function(x, y) x + y)
  End Function

  Private Function VisibleRows() As List(Of Row)
    Return Rows.Where(Function(x) x.Index >= FirstVisibleRow.Index AndAlso x.Index <= LastVisibleRow.Index).ToList
  End Function

  Private Sub Grid_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    e.Graphics.Clear(BackColor)
    'e.ClipRectangle

    Dim Left As Integer = -HScrollBar1.Value
    For Each Column As Column In Columns
      Left = Column.Draw(e.Graphics, Left)
    Next

    Dim Top As Integer = HeaderRow.Draw(e.Graphics)
    For Each Row As Row In VisibleRows()
      Top = Row.Draw(e.Graphics, Top)
    Next
  End Sub

  Private Sub Grid_Scroll(sender As Object, e As System.Windows.Forms.ScrollEventArgs) Handles Me.Scroll
    If e.ScrollOrientation = ScrollOrientation.VerticalScroll Then
      Select Case e.Type
        Case ScrollEventType.First
          FirstVisibleRow = Rows.First
        Case ScrollEventType.Last
          FirstVisibleRow = Rows(Rows.Last.Index - VisibleRowCount + 1)
        Case ScrollEventType.SmallDecrement
          FirstVisibleRow = Rows(Math.Max(FirstVisibleRow.Index - 1, 0))
        Case ScrollEventType.SmallIncrement
          FirstVisibleRow = Rows(Math.Min(FirstVisibleRow.Index + 1, Rows.Last.Index - VisibleRowCount + 1))
        Case ScrollEventType.LargeDecrement
          FirstVisibleRow = Rows(Math.Max(FirstVisibleRow.Index - VisibleRowCount, 0))
        Case ScrollEventType.LargeIncrement
          FirstVisibleRow = Rows(Math.Min(LastVisibleRow.Index, Rows.Last.Index - VisibleRowCount + 1))
      End Select
    End If
    Invalidate()
  End Sub

  Private Sub Grid_SizeChanged(sender As Object, e As System.EventArgs) Handles Me.SizeChanged
    VisibleGridSize = GetVisibleGridSize()
  End Sub

  Private Sub HScrollBar1_Scroll(sender As Object, e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll
    Invalidate()
  End Sub

End Class
4
  • "Think you showed us enough code there Butch?"
    – LarsTech
    Oct 17, 2014 at 23:50
  • Thats a lot of unnecessary code to post when the sub that is likely causing the problem is SetScrollBarVisibility(). I dont understand why the calculation for the Vertical Scroll Bar is different to the Horizontal one, why are you going - IIF for the vertical and + IIF for the horizontal? Oct 17, 2014 at 23:52
  • @LarsTech, ok I removed all I thought was irrelevant. Hope i didnt remove the problem code.
    – toddmo
    Oct 17, 2014 at 23:59
  • @JeremyThompson, don't worry about the vertical. it isn't implemented yet. thanks.
    – toddmo
    Oct 17, 2014 at 23:59

1 Answer 1

0

The AutoScroll property of my UserControl was getting set to True by a parent control. Once I set it back to False in the designer, my code works as expected 100% of the time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.