|
-
Nov 22nd, 2004, 11:28 PM
#1
Thread Starter
Fanatic Member
vb.net 2003: trying to speed up an owner-drawn listbox
I'll post only relevent code...
My intent here is a logging system that uses an owner drawn listbox (fixed item height) for custom item background colors (to denote the 'alert level' of the specific item). I'm using a custom class called 'ColorListItem' to store the Text and the Background color of the item:
VB Code:
Public Class ColorListItem
Public Text As String
Public Color As System.Drawing.Color
Public Sub New(ByVal NewText As String, ByVal NewColor As System.Drawing.Color)
Text = NewText
Color = NewColor
End Sub
End Class
Each item in the listbox is added in the format of:
VB Code:
lbConsole.Items.Add(New ColorListItem("Text", Drawing.Color.Red)
(Simply an example)
The listbox, lbConsole, has DrawMode set to OwnerDrawFixed so that the MeasureItem event isn't fired. I put the DrawItem event in the sub Log_DrawItem
VB Code:
Sub Log_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs)
Dim bg As SolidBrush
Dim cli As Object 'ColorListItem
'We use Object just incase the item was improperly added
If e.Index = -1 Then Exit Sub 'it looks good when there are no items, no drawing needed
cli = sender.Items(e.Index)
Try 'if cli isn't a valid ColorListItem
bg = New SolidBrush(cli.color)
Catch
bg = New SolidBrush(Drawing.Color.Yellow) 'highlight any problems
End Try
e.Graphics.FillRectangle(bg, e.Bounds)
e.Graphics.DrawString(cli.text, e.Font, New SolidBrush(Drawing.Color.Black), e.Bounds.X, e.Bounds.Y)
End Sub
If, for some reason, it's unable to use the Color property of 'cli', it defaults to yellow. (The scheme I'm using is red-based so it will stand out.)
I intend that the listbox forces it's color scheme over the standard windows colors cheme (i.e. color.black vs systemcolors.controltext)
I also intend that no selection is visible. Alas, I can not set the selection mode to none, becuase I have to be able to scroll to the most recently added item at the bottom (this is done elsewhere in the code).
The issue I'm noticing is that when the listbox adds an item forcing the listbox to scroll, it flickers horribly. I'd like to have it draw as if the drawmode was Normal.
I'm thinking the drawing each item from it's own thread might speed it up, but I can't figure out how to impliment that.
Any ideas to reduce the flicker?
-
Dec 3rd, 2004, 03:35 AM
#2
Hyperactive Member
Re: vb.net 2003: trying to speed up an owner-drawn listbox
<shot in the dark>
maybe using BeginUpdate and EndUpdate will help to reduce the flickering?
</shot in the dark>
Obey the dragon thing. Or not. Or possibly just a bit.
-
Dec 3rd, 2004, 03:58 AM
#3
Thread Starter
Fanatic Member
Re: vb.net 2003: trying to speed up an owner-drawn listbox
 Originally Posted by The Dutch Dude
<shot in the dark>
maybe using BeginUpdate and EndUpdate will help to reduce the flickering?
</shot in the dark>
Actually, that increased the flickering to a horrible level whereas it's just bad now.
The way I had coded it was also pretty bad. To make it compatible with the way I intend to do things now, I have to re-write the code for that and in doing so, I'm going to double-buffer the whole thing to see if that works.
-
Dec 3rd, 2004, 12:51 PM
#4
Hyperactive Member
Re: vb.net 2003: trying to speed up an owner-drawn listbox
Weird... BeginUpdate should stop the listbox from doing anything and display utterly nothing, a grey mass at the most, but it should not flicker. But I'll be interested to hear how this is solved..
Obey the dragon thing. Or not. Or possibly just a bit.
-
Dec 3rd, 2004, 07:30 PM
#5
Thread Starter
Fanatic Member
Re: vb.net 2003: trying to speed up an owner-drawn listbox
its mainly because the EndUpdate causes the whole thing to be re-drawn which causes all of the items to be redrawn. It doesn't call the drawitem event, but it does BeginUpdate...EndUpdate each time a new item is added or otherwise drawn.
-
Dec 4th, 2004, 04:17 AM
#6
Hyperactive Member
Re: vb.net 2003: trying to speed up an owner-drawn listbox
Ah, right, BeginUpdate should be placed before and EndUpdate should be placed after any adding takes place, then it should only redraw once, which is the point of the 2 methods, to stop the redrawing when an item is added.
So:
VB Code:
myBootaliciousListBox.BeginUpdate()
' example loop
while somethingOrOther
myBootaliciousListBox.Items.Add(New ColorListItem("Text", Drawing.Color.Red)
end while
myBootaliciousListBox.EndUpdate()
Obey the dragon thing. Or not. Or possibly just a bit.
-
Dec 5th, 2004, 12:28 AM
#7
Thread Starter
Fanatic Member
Re: vb.net 2003: trying to speed up an owner-drawn listbox
I'll give that a try...
 Originally Posted by The Dutch Dude
Ah, right, BeginUpdate should be placed before and EndUpdate should be placed after any adding takes place, then it should only redraw once, which is the point of the 2 methods, to stop the redrawing when an item is added.
So:
VB Code:
myBootaliciousListBox.BeginUpdate()
' example loop
while somethingOrOther
myBootaliciousListBox.Items.Add(New ColorListItem("Text", Drawing.Color.Red)
end while
myBootaliciousListBox.EndUpdate()
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|