Screen Flicker

mafrosis

Well-known member
Joined
Jun 5, 2006
Messages
88
Location
UK
Programming Experience
5-10
I have written a custom Panel class which calls a method to layout its controls in a certain way on 3 events - Resize, ControlAdded, ControlRemoved. It then calls Invalidate to invoke a repaint.

The problem is that I get a lot of screen flicker when using the component - my form also has a StatusBar at the bottom and this flickers too.

Ive used the double buffer code as described elsewhere - is there anything else I can do?

VB.NET:
Me.SetStyle(ControlStyles.DoubleBuffer, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, False)
Me.SetStyle(ControlStyles.ResizeRedraw, True)
Me.SetStyle(ControlStyles.UserPaint, True)
I can post more of the code if it would help..

Cheers
mafro
 
When you are adding controls to a container you should call suspend layout to prevent the parent from getting redrawn each time a control is added
 
On just re-reading your post, i'm not sure why the status bar should flicker as well. There must be something else going on here. It may be helpful if you post some more of your code.
 
When the flicker happens im not actually adding any controls to the form - the controls are added at startup and then then flicker occurs when the form is resized.

VB.NET:
Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
    LayoutControls()
    MyBase.OnResize(e)
End Sub

The actual LayoutControls method just loops thru Me.Controls and sets the Left and Top properties to tile the controls out into a grid. My assumption was that this setting of the controls' positions will happen before the Paint - therefore not being affected by SuspendLayout etc..
 
It's part of the custom Panel class - there's no code in the main Form apart from creating the custom Panel object and adding a few controls to it.
 
I find this a bit odd, i have tried to replicate what your problem but i cant get the flicker that you have spoken about. Is there anything else going on there?
 
Wait a minute.... Just re-read your first post...

SetStyle(ControlStyles.AllPaintingInWMPaint, False)

Should be..

SetStyle(ControlStyles.AllPaintingInWMPaint, True)
 
Thanks vis! Can't believe I missed that one - result of copy and pasting code I suspect..
 
Last edited:
I am having a silimar problem... actually the flickering part is the only similar part. I'm doing custom draw code in the Paint event, and getting a ton of flicker. I have seen sample code all over the internet recommending the same thing I see here, namely using the SetStyle method and the ControlStyles constants. Now, that is all well and good, but I can't seem to find this member function or it's constants. I've checked if it is a member of the Form, a member of the Panel, or a member of the Control base class. No luck. I'm using .NET framework 1.1, Visual Studio 2003. Is this something that was added in 2.0?
 
I think the flickering comes from overiding the Paint method - this is something im not sure on - but the SetStyle methods are hidden members of Control (you can use Reflector to find these) and should be put in the constructor of your component:

VB.NET:
Public Class MyPanel
    Inherits System.Windows.Forms.Panel
    
    Public Sub New()
        Me.SetStyle(ControlStyles.DoubleBuffer, True)
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        Me.SetStyle(ControlStyles.ResizeRedraw, True)
        Me.SetStyle(ControlStyles.UserPaint, True)
    End Sub
    
    ...
Actually, im still having flicker problems on my app - but ive got a deadline for a functional prototype so the UI's gonna have to wait.. I think this problem will be revisited soon :confused:
 
Back
Top