So I have a custom listbox that is displaying garbage text when scrolling horizontally. I need the custom listbox to stop an annoying flicker, but it's causing issues with the scroll. I think I could solve the issue if only I could access the hscroll event, but I don't know how to do that. I will put down my custom listbox class just in case you will need it to understand the problem.
Edit: I realized my question is a little vague - telling me how to access the hscroll event within the listbox will suffice
thanks much in advance.
Edit: I realized my question is a little vague - telling me how to access the hscroll event within the listbox will suffice
thanks much in advance.
VB.NET:
Public Class DoubleBufferedListBox
Inherits ListBox
Private resizing As Boolean
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = 5 Then
' WM_SIZE = 0x05
Me.resizing = True
MyBase.WndProc(m)
Me.resizing = False
Return
End If
MyBase.WndProc(m)
End Sub
Public Sub New()
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint, True)
Me.DrawMode = DrawMode.OwnerDrawFixed
End Sub
Protected Overrides Sub OnDrawItem(ByVal e As DrawItemEventArgs)
If e.Index = -1 OrElse (CInt(e.State) And &H1000000) = 0 AndAlso Me.resizing Then
Return
End If
Dim bgColor As Color = Color.White
If (e.Index Mod 2 = 1) Then
bgColor = Color.Beige
End If
Dim a As New DrawItemEventArgs(e.Graphics, e.Font, e.Bounds, e.Index, DrawItemState.Default, e.ForeColor, bgColor)
a.DrawBackground()
Dim myBrush As Brush = Brushes.Black
Dim sf As StringFormat = StringFormat.GenericTypographic
sf.LineAlignment = StringAlignment.Center
e.Graphics.DrawString(Items(e.Index).ToString(), _
a.Font, myBrush, a.Bounds, sf)
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim iRegion As New Region(e.ClipRectangle)
e.Graphics.FillRegion(New SolidBrush(Me.BackColor), iRegion)
If Me.Items.Count > 0 Then
For i As Integer = 0 To Me.Items.Count - 1
Dim irect As System.Drawing.Rectangle = Me.GetItemRectangle(i)
If e.ClipRectangle.IntersectsWith(irect) Then
If (Me.SelectionMode = SelectionMode.One AndAlso Me.SelectedIndex = i) OrElse (Me.SelectionMode = SelectionMode.MultiSimple AndAlso Me.SelectedIndices.Contains(i)) OrElse (Me.SelectionMode = SelectionMode.MultiExtended AndAlso Me.SelectedIndices.Contains(i)) Then
OnDrawItem(New DrawItemEventArgs(e.Graphics, Me.Font, irect, i, (If(Me.SelectedIndices.Contains(i), DrawItemState.Selected, _
DrawItemState.[Default])) Or DirectCast(&H1000000, DrawItemState), Me.ForeColor, Me.BackColor))
Else
OnDrawItem(New DrawItemEventArgs(e.Graphics, Me.Font, irect, i, (If(Me.SelectedIndices.Contains(i), DrawItemState.Selected, _
DrawItemState.[Default])) Or DirectCast(&H1000000, DrawItemState), Me.ForeColor, Me.BackColor))
End If
iRegion.Complement(irect)
End If
Next
End If
MyBase.OnPaint(e)
End Sub
End Class