Sluggish Listbox?-VBForums
Results 1 to 6 of 6

Thread: Sluggish Listbox?

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Location
    Maine, USA
    Posts
    3

    Sluggish Listbox?

    I've read multiple threads on this subject, but none of the ideas had any affect in my program.... But here it is.

    After realizing the multiple custom stuff you can do in a listbox, I decided to go custom and have the "ownerdrawn" option to be able to customize my listbox to my liking. And graphically, it's perfect, the only issue is when one is scrolling.

    Code:
    Private Sub ListBox1_Drawimage(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
            Dim align As New StringFormat
            align.Alignment = StringAlignment.Center
            e.DrawBackground()
            If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
                e.Graphics.FillRectangle(Brushes.LightGreen, e.Bounds)
            End If
            Using b As New SolidBrush(e.ForeColor)
                e.Graphics.DrawString(ListBox1.GetItemText(ListBox1.Items(e.Index)), e.Font, b, e.Bounds, align)
            End Using
            e.Graphics.DrawImage(My.Resources.jbar_png, 1, 365, ListBox1.Size.Width, 100)
        End Sub
    (jbar_png, is just an image that is for decoration for the listbox)
    Basically, this code for the custom listbox allows the custom doo dads that make my program to act quite perfect to my liking. Again, the issue is when a user scrolls down. The listbox redrawing is very sluggish (almost taking seconds to even show the user selected the next item down) as well because of the sluggish nature of the listbox, there could be multiple instances of jbar_png shown in the program. Currently, the listbox holds ~400 or so items, which is merely some text which is a reference for later use.

    If there's anything that'd be able to speed the listbox redrawing ability, please help. I'd like for the listbox to work smoothly and seamlessly if at all possible.


    Thanks,
    Comake

  2. #2
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    23,179

    Re: Sluggish Listbox?

    The one thing I see in there is that you create a new brush over and over, but is that necessary? Frankly, that shouldn't have enough impact to matter, but it's one small point.

    The larger point is this: When you scroll, how often does this code get drawn? If you are getting that much slowdown, it is likely that you are getting FAR more events than you expect to get. That could well be your problem, but you'd have to measure it. There are a couple ways. You could set a breakpoint in there, and adjust it (I think it's on the right-mouse menu) so that the breakpoint is only hit after N times. Then you could set N at something like 10. If it gets hit before you have scrolled even a single item, you know where your problem is. Another route would be to add an integer variable to the form and increment it in that routine. Then scroll it a little bit and see how high the number got to.

    The whole point is figuring out exactly where the problem is. Your code doesn't look likely to cause much of a slowdown unless it is being called quite often. If you find out that it is NOT being called often, then you could use the profiler class I put over in the .NET CodeBank to figure out where the slowdown is actually occuring. It only really works well for timing executing code, though, so you have to time in such a way to avoid the human delays that tend to come in any user interactions.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Location
    Maine, USA
    Posts
    3

    Re: Sluggish Listbox?

    That's definitely where the problem lies. I checked it and with one scroll it runs 21 times. Then just simply moving from one item to another it goes 6. How would I be able to limit the amount of times it redraws?

  4. #4
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    23,179

    Re: Sluggish Listbox?

    That's going to be tricky. I have no specific solution, and would have to try out a variety myself. One of the easiest might be to simply check whether the index of the top item has changed, and not redraw until it does. This could be done in that method by checking the top item (I don't see a method that gets the first item shown, which exists for a datagridview, but perhaps not for the older Listbox, but you could use the GetIndexFromPoint method and pass in a point near the top of the box), and exit the sub if that hasn't changed. However, that will probably only work when the user is scrolling the listbox, and might cause visual problems for other situations (re-sizing the form or control, refreshing the whole form after another form was in front of it, and so forth).

    The other option is the BeginUpdate/EndUpdate methods. Exactly when you would call Begin is hard to say, and since it would halt painting it would cause the listbox to have some visual irregularities while the user was scrolling.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Location
    Maine, USA
    Posts
    3

    Re: Sluggish Listbox?

    I've redone some of the code to find where the sluggish really happens. it seems to be where jbar_png is drawn. Is there any way I would be able to have the listbox redraw the jbar_png faster? Or could the file size / resolution be also affecting the drawing slowness?

  6. #6
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    23,179

    Re: Sluggish Listbox?

    I have found that there is an impact if the image gets to a certain size, but the size I was seeing this at was when I went to 512x1024 images. Sizes of 256x512 were only very slightly slower than 128x256, and I couldn't see a difference below that size. It seems like your images are smaller.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.