You could if you wanted make a double buffered ListBox by inheriting ListBox and setting DoubleBuffered=True in the constructor. But are you actually getting any flickering? There's nothing in the posted painting code (different brushes, different fonts etc.) that could cause flickering any more than drawing a standard ListBox item. The Try-Catch looks suspect because catching an exception would cause a lag in rendering. But I can't see how that could ever be triggered -- unless you were misguidedly trying to call the sub directly instead of letting the system call it as an event handler, in which case anything could go wrong and double buffering won't help.

VicJ
 
Unfortunately, even with the double-buffered listbox, I still get flickering when resizing the form :(
I can't see why it happens but try setting DoubleBuffered=True as I suggested. ControlStyles.OptimizedDoubleBuffer only works in combination with another ControlStyle (UserDraw if I remember correctly). But the use of SetStyle statements for this purpose was superseded by the DoubleBuffered property in VB2005.

By the way, if you are customizing an inherited control you should put the custom code in the On.. sub instead of handling the Event directly. Type Protected Overrides Sub OnDrawItem and Visual Studio will generate the empty sub; insert your present code before the automatic call to MyBase.OnDrawItem. This is just a conventional pattern for control customization, so I doubt it will make an observable difference as regards flickering.

VicJ

AFTERTHOUGHT: You could also try setting the Form's DoubleBuffered property to True in the Designer or in Code. But it would be preferable to solve the problem in the control itself, if possible.
 
Last edited:
Do you have the listbox anchored or docked so that it resizes whenever you resize the form? I have been doing a few tests and it seems that even a well-filled standard ListBox can flicker a bit when you resize it, and a custom drawn one seems to be even more prone to laggy redrawing. Making the Form double buffered helps a tiny bit, but making the ListBox double buffered doesn't at all! So I have to pull back from my earlier suggestions. Never mind, here's a way that may eliminate flickering caused by resizing the Form. Add this code to the form:

VB.NET:
    Private Sub Form1_ResizeBegin(sender As Object, e As System.EventArgs) Handles Me.ResizeBegin
        Me.SuspendLayout()
    End Sub

    Private Sub Form1_ResizeEnd(sender As Object, e As System.EventArgs) Handles Me.ResizeEnd
        Me.ResumeLayout(True)
    End Sub

The effect is to put off changing the size of any Docked or Anchored controls until you release the mouse. Does it solve your problem?

If you decide to go ahead with making a custom (inherited) ListBox control which you can put in the VS ToolBox, then there are some changes needed. You shouldn't refer to the host form from the control class because that will cause problems when an instance of the control is used on a different form. For example, replace frmKHSSfrmKHSS.dbLstSongs.Font by Me.Font. Similarly, replace frmKHSS.dbLstSongs.Items.Item(e.Index) by Me.Items(e.Index). Then when using an instance of the class, set the control's own Font and Items properties in the Designer or in code. But if you only need it once, you may prefer to go back to coding the DrawItem event handler as in your first post.

That looks like a bold attempt to use BufferedGraphics to implement custom double buffering but I don't think it will work any better than standard double buffering. It could be of some benefit when correctly applied in a multithreading situation, but I have never seen it used convincingly. As to the code you have added to the form in your last post ... yuck:cocksure:!

VicJ
 

Latest posts

Back
Top