473,396 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Owner Draw Listbox - Performance?

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
Nov 20 '05 #1
4 4322
Hi James,

I tested your code on my machine with the Orders table in the Northwind
database.
The performance is just one second on my machine similar with the ListBox.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim o As DateTime
o = Now
Dim lstbx As ColoredListBox
SqlDataAdapter1.Fill(Me.DataSet31)
lstbx = New ColoredListBox
lstbx.DrawMode = DrawMode.OwnerDrawFixed
Me.Controls.Add(lstbx)
lstbx.DataSource = Me.DataSet31.Orders
lstbx.DisplayMember = "ShipName"
lstbx.ChangeBackgroundMember = "ShipVia"
lstbx.ChangeBackgroundColor = Color.Red
Dim ts As New TimeSpan(Now.Ticks - o.Ticks)
MsgBox(ts.Ticks)
MsgBox(ts.Seconds)
End Sub

In the ColoredListBox, I just change one code line to adapt my test
table(Orders)

If myRow(ChangeBackgroundMember) >= 1 Then

and this will cause the ColoredListBox render about 800 rows of data in
ColoredListBox.

You may create a new project and try my code to see if the problem persist.
My test environment is PIII733, 512MB RAM.
If you have any concern on this issue, please post here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 20 '05 #2
Good Morning Peter,

On my application, I am using the listbox on a sub-form (i.e. modal pop-up).
When the pop up is loaded using the standard listbox, as soon as the page
appears, all data is already showing in the listbox (very fast load). When
I use the ColoredListbox in the pop up in place of the Listbox, the time is
very close using your measuring method to show the pop-up screen; however,
when the page is rendered the listbox does not have any data in it yet, and
it seems to take a couple seconds 3-4 before the data slowly gets rendered
in the listbox and the items appear.

Once it has been created, it appears that the scrolling using the scroll
bars is about the same as the listbox.

What could be causing the delay between when the ColoredListbox is first
presented empty, and the slow actual rendering of the items in the listbox?
Is there another override that I missed that I need to do in my class?

Thanks!

Jim

"Peter Huang" <v-******@online.microsoft.com> wrote in message
news:fH**************@cpmsftngxa07.phx.gbl...
Hi James,

I tested your code on my machine with the Orders table in the Northwind
database.
The performance is just one second on my machine similar with the ListBox.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim o As DateTime
o = Now
Dim lstbx As ColoredListBox
SqlDataAdapter1.Fill(Me.DataSet31)
lstbx = New ColoredListBox
lstbx.DrawMode = DrawMode.OwnerDrawFixed
Me.Controls.Add(lstbx)
lstbx.DataSource = Me.DataSet31.Orders
lstbx.DisplayMember = "ShipName"
lstbx.ChangeBackgroundMember = "ShipVia"
lstbx.ChangeBackgroundColor = Color.Red
Dim ts As New TimeSpan(Now.Ticks - o.Ticks)
MsgBox(ts.Ticks)
MsgBox(ts.Seconds)
End Sub

In the ColoredListBox, I just change one code line to adapt my test
table(Orders)

If myRow(ChangeBackgroundMember) >= 1 Then

and this will cause the ColoredListBox render about 800 rows of data in
ColoredListBox.

You may create a new project and try my code to see if the problem persist. My test environment is PIII733, 512MB RAM.
If you have any concern on this issue, please post here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 20 '05 #3
Hi James,

It seems that the performance hit occurs in the ownerdraw function.
You may try to change your code as follows.

Dim brush As SolidBrush
Public Sub New()
MyBase.New()
brush = New SolidBrush(Color.Black)
End Sub

Private Sub ColoredListBox_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles MyBase.DrawItem
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) >= 1 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.Color = 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.Color = myForeColor
e.Graphics.DrawString(myRow(Me.DisplayMember), Me.Font, brush,
e.Bounds.X, e.Bounds.Y)
e.DrawFocusRectangle()
myRow = Nothing
End Sub

You may also try to test with fewer datarow in your coloredlistbox to see
if the problem persists.
I have also tested with a popup form, the render will complete less than
one second after the form is show on about 8000 rows data.
Did the problem occurs when you popup your form with coloredlistbox on the
second time?
(i.e. you click a button will popup the form with coloredlistbox , close
the form and popup it again)
If you have any concern on this issue, please post here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 20 '05 #4
Peter,

The application is working fine now. It seems like if I recompiled the
application as Release versus Debug, many of the performance issues went
away. This type of performance change between release and debug was not
something that I had experienced before.

So, for now, I would say that this is fine!

Thanks for the help!

Jim

"James Radke" <jr*****@wi.rr.com> wrote in message
news:OV**************@TK2MSFTNGP12.phx.gbl...
Good Morning Peter,

On my application, I am using the listbox on a sub-form (i.e. modal pop-up). When the pop up is loaded using the standard listbox, as soon as the page
appears, all data is already showing in the listbox (very fast load). When I use the ColoredListbox in the pop up in place of the Listbox, the time is very close using your measuring method to show the pop-up screen; however,
when the page is rendered the listbox does not have any data in it yet, and it seems to take a couple seconds 3-4 before the data slowly gets rendered
in the listbox and the items appear.

Once it has been created, it appears that the scrolling using the scroll
bars is about the same as the listbox.

What could be causing the delay between when the ColoredListbox is first
presented empty, and the slow actual rendering of the items in the listbox? Is there another override that I missed that I need to do in my class?

Thanks!

Jim

"Peter Huang" <v-******@online.microsoft.com> wrote in message
news:fH**************@cpmsftngxa07.phx.gbl...
Hi James,

I tested your code on my machine with the Orders table in the Northwind
database.
The performance is just one second on my machine similar with the ListBox.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim o As DateTime
o = Now
Dim lstbx As ColoredListBox
SqlDataAdapter1.Fill(Me.DataSet31)
lstbx = New ColoredListBox
lstbx.DrawMode = DrawMode.OwnerDrawFixed
Me.Controls.Add(lstbx)
lstbx.DataSource = Me.DataSet31.Orders
lstbx.DisplayMember = "ShipName"
lstbx.ChangeBackgroundMember = "ShipVia"
lstbx.ChangeBackgroundColor = Color.Red
Dim ts As New TimeSpan(Now.Ticks - o.Ticks)
MsgBox(ts.Ticks)
MsgBox(ts.Seconds)
End Sub

In the ColoredListBox, I just change one code line to adapt my test
table(Orders)

If myRow(ChangeBackgroundMember) >= 1 Then

and this will cause the ColoredListBox render about 800 rows of data in
ColoredListBox.

You may create a new project and try my code to see if the problem

persist.
My test environment is PIII733, 512MB RAM.
If you have any concern on this issue, please post here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Patty O'Dors | last post by:
Hi I have some code to create an ownerdrawn listbox (derived), and when I add an item to it, the bold text of the first item (the title, 'Collections and Maturities') mysteriously seems to get...
1
by: Tim Haughton | last post by:
Like most fans of eye candy, I've always thought the standard MainMenu control was a bit drab. After looking at third party components, I decided to see if I could brute force it into looking nice....
12
by: Jim H | last post by:
I have a ListBox hold a list of my custom controls (it's UserControl based control with a couple radio buttons and a checkbox). Everything works and displays the way I expected it to. I can move...
0
by: John R. | last post by:
I want to add a textbox next to each item in a Listbox on a Windows Form. The items need to be scrollable so I thought that maybe this is possible with an Owner Draw listbox. Searching the web...
7
by: Devron Blatchford | last post by:
Hi there, I have created an owner draw menu item using DrawItem and MeasureItem in VB.NET. This seems to work well. I was wondering how I can do mouse over effects in these menus. Basically the...
0
by: Yair Cohen | last post by:
Hi all ! I created a control derived from the listBox to customize it and make it multi column. I use DrawString to draw the text and, i see it in Win2000 and WinXP, but, i can't see it on a...
1
by: Robin Tucker | last post by:
Hi ppl, My owner draw list box controls do not "refresh" old selected items when a new selection is made. This means that as you click to make selections, the previously selected items stay...
2
by: dan heskett | last post by:
I am owner-drawing a listbox, in an attempt to create a nice list with some custom "fields" and text layout. Essentially it works, but I must be missing something big, conceptually, because I...
0
by: LostInMd | last post by:
Hi All, I've got an owner drawn listBox where I draw and measure the items that I add to the listBox. For example, I have a listBox that can only display 10 characters on each horizontal line. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.