Solved

How to stop excess flickering implementing gradient shading on a form?

Posted on 2011-09-11
3
376 Views
Last Modified: 2012-05-12
Hello,

I have implemented gradient shading on the forms in my program using a method I have found on a number of sites:

Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)

        If My.Settings.Gradient_Shading = False Then Exit Sub

        Dim formGraphics As Graphics = e.Graphics

        Dim gradientBrush As New LinearGradientBrush(New Point(0, 0), New Point(0, Height), My.Settings.GradientColor_1, My.Settings.GradientColor_2)

        formGraphics.FillRectangle(gradientBrush, ClientRectangle)

    End Sub


This worked, but I found that in order to have the same shading on pages within a tab control on that form, I had to implement the following code:


Private Sub tbpOptimizer_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) _
          Handles tbpOptimizer.Paint

        Dim rect As New System.Drawing.Rectangle(0, 0, sender.Width, sender.Height)

        Dim g As Graphics = e.Graphics

        Dim gradientBrush As New LinearGradientBrush(New Point(0, 0), New Point(0, sender.Height), My.Settings.GradientColor_1, My.Settings.GradientColor_2)

        g.FillRectangle(gradientBrush, rect)

    End Sub


This does work, but at times, the form flickers for a few seconds before the painting is done.  In trying to figure out what is happening, I found that the Paint event for the tab above fired about 80 times when that tab was selected!  I assume that is the cause of the flickering, because other tabs on the same Tab Control have their Paint event fired fewer times, and they flicker less.  But why would a tab page have to fire so many times?  And, more importantly, is there a better way for me to implement gradient shading onto my program screens?  

Thank you in advance.

Vijay
0
Comment
Question by:vdurbal
3 Comments
 
LVL 15

Expert Comment

by:x77
ID: 36521318
You can optimize using Double Buffer, but I think this in´nt the problem. If you do it, your remove Flicker but hide an inner problem.

I suggest a :

   Debug.Print "Paint " & Sender.ToString

at end of syour tbpOptimizer_Paint method.
I think you are painting twice.
By sample on Form.OnPaint and event suscription for Paint event.

Note. You wrote "sender.Width". You do´nt use "Option Strict". It saves a lot of code errors.
Take it at Project Properties on compiler Tab.
For new Project you can change Tools - Options - Project and Solutions - Default values for Vb (option Strict= ON).
I know that Option Strict demands more code (Ctype, Directcast ...), but it is the best option.
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 36523469
First of all, take x77 advice about Option Strict. This is the best thing you can offer yourself in this Visual Basic world. The little pain of having to cast more often is nothing to the hours of debugging it can save you.

Second, since you are handling the background, I would put the code in OnPaintBackground instead of OnPaint.

But, this would not solve your problem. The problem comes from the fact that OnPaint and Paint are called for every control that gets drawn on the container. The more controls you have on the container, the more flickering you get. You need to have something that draws very fast, otherwise you get the flickering.

DoubleBuffering helps a little, but not much.

I have tried dozens of ways to prevent that, but since its controlled at the framework level, there is nothing to do. No matter what method you use to delay the call to the painting events, as soon as you enable them, its as if a Refresh is sent to all the controls on the TabControl, and this falls through the TabControl that repaints itself for each of its child.

I am not even sure that there is a solution. The TabControl is one of the few controls that cannot have transparency, so I think that even the guys at Microsoft were not able to solve it.

This one is the worst problem I had in 40 years of programming. In my case, the background is a textured bitmap, so it is even slower than with a gradient. If somebody could come up with a solution, I would give him all the points I have accumulated in the last month (symbollically only, since this is not possible :-)).
0
 

Author Closing Comment

by:vdurbal
ID: 36537546
Thanks for your help.  At least I won't spend more time trying to fix this!
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
get combo value in class 5 14
Add a Condition in ASP Repeater 6 34
Tracing a live website down to the files which support it 2 26
Where is this file? 3 26
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question