Scroll event for the ListBox control

I have a ListBox control on a form in a Windows Forms app. The list contains more items than it can display, so the vertical scroll bar appears automatically. My question is, does any event fire when a user scrolls the list

I know the TopIndex property reflects the index of the top-most displayed item, but it doesn't appear to have a change event associated with it either. If there are no events that fire when the displayed list has changed (Layout and Paint don't work), does anyone have any ideas on how to detect that in code

TIA-



Answer this question

Scroll event for the ListBox control

  • greenie

    Thanks Larry, works like a charm. I needed to sync two listboxes, and this took care of it. Never would have figured that out in 10^6 years... Thanks again-


  • Transco901

    This may not be what your after, but if you change the list box to owner drawn fixed and implement the method below (adpated from the online help) it will certainly be called at least as often as scrolling occurs. Detecting the scroll would be simply a matter of watching topindex vs the last known value of topindex

    Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem

    e.DrawBackground()

    Using myBrush As New SolidBrush(ListBox1.ForeColor)

    e.Graphics.DrawString(ListBox1.Items(e.Index), e.Font, myBrush, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))

    e.DrawFocusRectangle()

    End Using

    End Sub



  • Scroll event for the ListBox control