473,769 Members | 2,078 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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):

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
Nov 20 '05 #1
4 4351
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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Dim o As DateTime
o = Now
Dim lstbx As ColoredListBox
SqlDataAdapter1 .Fill(Me.DataSe t31)
lstbx = New ColoredListBox
lstbx.DrawMode = DrawMode.OwnerD rawFixed
Me.Controls.Add (lstbx)
lstbx.DataSourc e = Me.DataSet31.Or ders
lstbx.DisplayMe mber = "ShipName"
lstbx.ChangeBac kgroundMember = "ShipVia"
lstbx.ChangeBac kgroundColor = Color.Red
Dim ts As New TimeSpan(Now.Ti cks - o.Ticks)
MsgBox(ts.Ticks )
MsgBox(ts.Secon ds)
End Sub

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

If myRow(ChangeBac kgroundMember) >= 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.m icrosoft.com> wrote in message
news:fH******** ******@cpmsftng xa07.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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Dim o As DateTime
o = Now
Dim lstbx As ColoredListBox
SqlDataAdapter1 .Fill(Me.DataSe t31)
lstbx = New ColoredListBox
lstbx.DrawMode = DrawMode.OwnerD rawFixed
Me.Controls.Add (lstbx)
lstbx.DataSourc e = Me.DataSet31.Or ders
lstbx.DisplayMe mber = "ShipName"
lstbx.ChangeBac kgroundMember = "ShipVia"
lstbx.ChangeBac kgroundColor = Color.Red
Dim ts As New TimeSpan(Now.Ti cks - o.Ticks)
MsgBox(ts.Ticks )
MsgBox(ts.Secon ds)
End Sub

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

If myRow(ChangeBac kgroundMember) >= 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(Colo r.Black)
End Sub

Private Sub ColoredListBox_ DrawItem(ByVal sender As Object, ByVal e As
System.Windows. Forms.DrawItemE ventArgs) 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.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(colo rs)
If myRow(ChangeBac kgroundMember) >= 1 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.Color = myBackColor
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.Color = myForeColor
e.Graphics.Draw String(myRow(Me .DisplayMember) , Me.Font, brush,
e.Bounds.X, e.Bounds.Y)
e.DrawFocusRect angle()
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******** ******@TK2MSFTN GP12.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.m icrosoft.com> wrote in message
news:fH******** ******@cpmsftng xa07.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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Dim o As DateTime
o = Now
Dim lstbx As ColoredListBox
SqlDataAdapter1 .Fill(Me.DataSe t31)
lstbx = New ColoredListBox
lstbx.DrawMode = DrawMode.OwnerD rawFixed
Me.Controls.Add (lstbx)
lstbx.DataSourc e = Me.DataSet31.Or ders
lstbx.DisplayMe mber = "ShipName"
lstbx.ChangeBac kgroundMember = "ShipVia"
lstbx.ChangeBac kgroundColor = Color.Red
Dim ts As New TimeSpan(Now.Ti cks - o.Ticks)
MsgBox(ts.Ticks )
MsgBox(ts.Secon ds)
End Sub

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

If myRow(ChangeBac kgroundMember) >= 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
2893
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 bunched up at the right, i.e. squashed up! any idea why? The main bit of the code is as such // (in progressReporter.cs) protected struct LBRow //a row of the listbox, whether it be the title or a
1
3181
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. I set the owner draw on the menu items to true, and draw the MenuItems differently depending on whether or not their selected, thus providing nice Outlook 2003 menu styling. The only problem is, there is no 'unselected' event, so I can't...
12
3491
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 through the list by dragging the scroll bar and it works fine, but if I try to use the up/down scroll bar buttons or page by clicking above or below the scroll thumb button the list does not scroll. Here's my OnDrawItem for the ListBox: ...
0
1815
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 and newsgroups I don't find an example anywhere how I could add a textbox or other control to an Owner Draw listbox. Can someone show me how I'd add a textbox to each listbox item using an Owner Draw listbox?
7
3943
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 same as the dot net menus where they get highlighted when the mouse moves over them. Does anyone have any VB dot net examples or suggestions on how I could do this?
0
912
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 computer running Win98, I use attribute to pass the colora as RGB values, and in the build method of the list, i convert it to Color by using FromARGB method. I thought that it because the alphablend that not supported in Win98, but the strange...
1
3569
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 highlighted along with the new ones too. It draws correctly when I minimize the window and then maximise it again, but I just don't seem to be getting a "DrawItem" event for switching of an item from Selected to NotSelected! Any ideas??? Here is...
2
3136
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 get all kinds of screen artifacts and weirdness. My general goal is: list item with a few areas for text, every other item shaded a light color for readability, font color changes with selection. The listbox is populated with custom structurs...
0
2688
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. The majority of my items contain much more than 10 characters and thus the reason for my use of owner drawn listBoxes - I do not want to use a horizontal scrollbar, instead I want the text of each item to wrap onto multiple lines. I do use a...
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10208
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9987
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9857
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5294
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3952
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3558
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.