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?




Reply With Quote