.NET Listbox bug?

Feb 11, 2005
Programming Experience
When I run the following program, the listbox does not scroll correctly. When clicking below the tab on the scroll bar (i.e. quickly scrolling) the scroll animation appears backwards: When scrolling down, the items should appear to move up, but they move down.

Anyone have some insight?

To see what I mean, copy and paste the following code in a new Windows Application project:

Friend WithEvents ListBox1 As System.Windows.Forms.ListBox

Private Sub InitializeOwnerDrawnListBox()
Me.ListBox1 = New System.Windows.Forms.ListBox
ListBox1.DrawMode = DrawMode.OwnerDrawVariable
ListBox1.Location = New Point(20, 20)
ListBox1.Size = New Size(240, 140)

Dim i As Integer
For i = 1 To 20 ' Populate the ListBox
End Sub

' Handle the DrawItem event for an owner-drawn ListBox.
Private Sub ListBox1_DrawItem(ByVal sender As Object, _
ByVal e As DrawItemEventArgs) Handles ListBox1.DrawItem

If e.Index < 0 Then Exit Sub

' If the item is the selected item, then draw the rectangle filled in blue.
If (e.State And DrawItemState.Selected = DrawItemState.Selected) Then
e.Graphics.FillRectangle(Brushes.CornflowerBlue, e.Bounds)
e.Graphics.FillRectangle(Brushes.White, e.Bounds)
End If

' Draw the text in the item.
e.Graphics.DrawString(Me.ListBox1.Items(e.Index), Me.Font, _
Brushes.Black, e.Bounds.X, e.Bounds.Y)

' Draw the focus rectangle around the selected item.
End Sub

' Handle the MeasureItem event for an owner-drawn ListBox.
Private Sub ListBox1_MeasureItem(ByVal sender As Object, _
ByVal e As MeasureItemEventArgs) Handles ListBox1.MeasureItem

e.ItemHeight = 13
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Latest posts

Top Bottom