Results 1 to 7 of 7

Thread: vb.net 2003: trying to speed up an owner-drawn listbox

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    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:
    1. Public Class ColorListItem
    2.     Public Text As String
    3.     Public Color As System.Drawing.Color
    4.  
    5.     Public Sub New(ByVal NewText As String, ByVal NewColor As System.Drawing.Color)
    6.         Text = NewText
    7.         Color = NewColor
    8.     End Sub
    9. End Class

    Each item in the listbox is added in the format of:
    VB Code:
    1. 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:
    1. Sub Log_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs)
    2.         Dim bg As SolidBrush
    3.         Dim cli As Object 'ColorListItem
    4.         'We use Object just incase the item was improperly added
    5.  
    6.         If e.Index = -1 Then Exit Sub 'it looks good when there are no items, no drawing needed
    7.  
    8.         cli = sender.Items(e.Index)
    9.  
    10.         Try 'if cli isn't a valid ColorListItem
    11.             bg = New SolidBrush(cli.color)
    12.         Catch
    13.             bg = New SolidBrush(Drawing.Color.Yellow) 'highlight any problems
    14.         End Try
    15.  
    16.         e.Graphics.FillRectangle(bg, e.Bounds)
    17.         e.Graphics.DrawString(cli.text, e.Font, New SolidBrush(Drawing.Color.Black), e.Bounds.X, e.Bounds.Y)
    18.     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?

  2. #2
    Hyperactive Member The Dutch Dude's Avatar
    Join Date
    Nov 2001
    Location
    Holland. Land of the Dutch. Home for me.
    Posts
    261

    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.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    Re: vb.net 2003: trying to speed up an owner-drawn listbox

    Quote 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.

  4. #4
    Hyperactive Member The Dutch Dude's Avatar
    Join Date
    Nov 2001
    Location
    Holland. Land of the Dutch. Home for me.
    Posts
    261

    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.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    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.

  6. #6
    Hyperactive Member The Dutch Dude's Avatar
    Join Date
    Nov 2001
    Location
    Holland. Land of the Dutch. Home for me.
    Posts
    261

    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:
    1. myBootaliciousListBox.BeginUpdate()
    2.  
    3. ' example loop
    4. while somethingOrOther
    5.   myBootaliciousListBox.Items.Add(New ColorListItem("Text", Drawing.Color.Red)
    6. end while
    7.  
    8. myBootaliciousListBox.EndUpdate()
    Obey the dragon thing. Or not. Or possibly just a bit.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    Re: vb.net 2003: trying to speed up an owner-drawn listbox

    I'll give that a try...

    Quote 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:
    1. myBootaliciousListBox.BeginUpdate()
    2.  
    3. ' example loop
    4. while somethingOrOther
    5.   myBootaliciousListBox.Items.Add(New ColorListItem("Text", Drawing.Color.Red)
    6. end while
    7.  
    8. 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
  •  



Click Here to Expand Forum to Full Width