J
James Radke
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):
ChangeBackgroundMember = a bound data field which contains a boolean as
to whether this record should get a special background color
ChangeBackgroundColor = 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 _ChangeBackgroundMember As String
Private _ChangeBackgroundColor As Color
Public Property ChangeBackgroundMember() As String
Get
Return _ChangeBackgroundMember
End Get
Set(ByVal Value As String)
_ChangeBackgroundMember = Value
End Set
End Property
Public Property ChangeBackgroundColor() As Color
Get
Return _ChangeBackgroundColor
End Get
Set(ByVal Value As Color)
_ChangeBackgroundColor = Value
End Set
End Property
Private Sub ColoredListBox_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) 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.items(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.DrawBackground()
'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.Selected) = DrawItemState.Selected
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(ChangeBackgroundMember) Then
myBackColor = ChangeBackgroundColor
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(myBackColor)
e.Graphics.FillRectangle(brush, e.Bounds)
' Create a brush of the foreground color and draw the text using the
value of the display field
brush = New SolidBrush(myForeColor)
e.Graphics.DrawString(myRow(Me.DisplayMember), Me.Font, brush,
e.Bounds.X, e.Bounds.Y)
e.DrawFocusRectangle()
myRow = Nothing
End Sub
End Class
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):
ChangeBackgroundMember = a bound data field which contains a boolean as
to whether this record should get a special background color
ChangeBackgroundColor = 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 _ChangeBackgroundMember As String
Private _ChangeBackgroundColor As Color
Public Property ChangeBackgroundMember() As String
Get
Return _ChangeBackgroundMember
End Get
Set(ByVal Value As String)
_ChangeBackgroundMember = Value
End Set
End Property
Public Property ChangeBackgroundColor() As Color
Get
Return _ChangeBackgroundColor
End Get
Set(ByVal Value As Color)
_ChangeBackgroundColor = Value
End Set
End Property
Private Sub ColoredListBox_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) 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.items(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.DrawBackground()
'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.Selected) = DrawItemState.Selected
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(ChangeBackgroundMember) Then
myBackColor = ChangeBackgroundColor
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(myBackColor)
e.Graphics.FillRectangle(brush, e.Bounds)
' Create a brush of the foreground color and draw the text using the
value of the display field
brush = New SolidBrush(myForeColor)
e.Graphics.DrawString(myRow(Me.DisplayMember), Me.Font, brush,
e.Bounds.X, e.Bounds.Y)
e.DrawFocusRectangle()
myRow = Nothing
End Sub
End Class