Hello,
I am creating an owner draw listbox for a windows application. It is all
working, except the performance is significantly slower than the standard
listbox. Basically what I have done is added two new properties (full
source below):
ChangeBackgroun dMember = a bound data field which contains a boolean as
to whether this record should get a special background color
ChangeBackgroun dColor = the color to use when the above mentioned flag
is set to true
Can someone tell me what I can do to improve the performance of this
modified databound listbox?
Thanks!
=============== =============== ========== source code
=============== =============== =============== =============== ========
Imports System.Drawing
Imports System.Drawing. Drawing2D
Public Class ColoredListBox
Inherits ListBox
Private _ChangeBackgrou ndMember As String
Private _ChangeBackgrou ndColor As Color
Public Property ChangeBackgroun dMember() As String
Get
Return _ChangeBackgrou ndMember
End Get
Set(ByVal Value As String)
_ChangeBackgrou ndMember = Value
End Set
End Property
Public Property ChangeBackgroun dColor() As Color
Get
Return _ChangeBackgrou ndColor
End Get
Set(ByVal Value As Color)
_ChangeBackgrou ndColor = Value
End Set
End Property
Private Sub ColoredListBox_ DrawItem(ByVal sender As Object, ByVal e As
System.Windows. Forms.DrawItemE ventArgs) Handles MyBase.DrawItem
Dim brush As Brush
Dim selected As Boolean
Dim displayText As String
' Get the current data row in the bound datatable
Dim myRow As DataRowView = CType(sender.it ems(e.Index), DataRowView)
' The following method should generally be called before drawing.
' It is actually superfluous here, since the subsequent drawing
' will completely cover the area of interest.
e.DrawBackgroun d()
'Declare backcolor and forecolor temporary variables
Dim myBackColor As Color
Dim myForeColor As Color
' if the item is selected, then show it as darkblue background with
white text
If (e.State And DrawItemState.S elected) = DrawItemState.S elected
Then
myBackColor = Color.DarkBlue
myForeColor = Color.White
Else
' if the field indicated by the properties is true, then set the
background color to
' the specified color, otherwise use the default background and
foreground colors
If myRow(ChangeBac kgroundMember) Then
myBackColor = ChangeBackgroun dColor
myForeColor = Color.Black
Else
myBackColor = Me.BackColor
myForeColor = Me.ForeColor
End If
End If
' Create a brush of the background color, and fill the field area
brush = New SolidBrush(myBa ckColor)
e.Graphics.Fill Rectangle(brush , e.Bounds)
' Create a brush of the foreground color and draw the text using the
value of the display field
brush = New SolidBrush(myFo reColor)
e.Graphics.Draw String(myRow(Me .DisplayMember) , Me.Font, brush,
e.Bounds.X, e.Bounds.Y)
e.DrawFocusRect angle()
myRow = Nothing
End Sub
End Class