Solved

how to suppress the call to OnPaintBackGround of a control that I created... It is called tons of times unnecessarily.

Posted on 2014-09-15
4
322 Views
Last Modified: 2014-09-16
I created a panel control based on some code I got from the net, it works fine, however, when displayed it causes the screen to flicker, and I detected that the method OnPaintBackGround is called repeatedlybefore the form containg the control is displayed.
There is no explicit call to this method, and I do not know what event caused it to fire.

The method is an override to the base class and starts by calling
MyBase.OnPaintBackground(e)

I even commented out the call to the baseclass, and even removed all controls from the panel. The behaviour is as is. Can some expert please help?. Thanks
0
Comment
Question by:fmichail
  • 2
  • 2
4 Comments
 
LVL 40
ID: 40325144
It depends on what is causing the repeated calls, when it is called, what happens in the base class, and what you do in the method.

Double buffering can sometimes help with flicker, but because I keeps a bitmap of the control in memory, it requires more ressources. You can add that to the constructor.

You might try a call to SuspendLayout before the controls are drawn. When all the controls have been added to the screen, you call ResumeLayout followed by PerformLayout. This prevents the control from being redrawn each time a new Control is added to it.

I have encountered a situation where none of these worked. I that cases, I did nothing in the OnPaintBackground event but start a Timer for a short period. Repetitive calls simply resetted the Timer before it had time to fire. After the last call, the Timer was able to run to completion and trigger its Tick event, where I had put the code that would usually run in the OnPaintBackground.
0
 

Author Comment

by:fmichail
ID: 40325632
JamesBurger, Thanks for the reply, The following is my constructor (after I added what you suggested regarding suspendLayout, and resumelayout, in addition to the doublebuffering. The problem still exists.

    Public Sub New()
        MyBase.New()
        Me.DoubleBuffered = True
        Me.SuspendLayout()
        Me.SetDefaultControlStyles()
        Me.customInitialisation()
        Me.ResumeLayout(True)
    End Sub

I would like to go for your last solution (using timer). How to do that? If I move the contents of the OnPaintBackground method to the timer handler, how to create the paintEventArg e which is used as a parameter to OnPaintBackground.

Here is the code of OnPaintBackground method code

Protected Overrides Sub OnPaintBackGround(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaintBackground(e)
        If DesignMode = False Then
            If _loading = True Then
                Exit Sub
            End If
            _loading = True
        End If
        e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias

        Dim graphPath As System.Drawing.Drawing2D.GraphicsPath
        graphPath = Me.GetPath()

        '      Create Gradient Brush (Cannot be width or height 0)
        Dim filler As System.Drawing.Drawing2D.LinearGradientBrush
        Dim rect As System.Drawing.Rectangle = Me.ClientRectangle

        If Me.ClientRectangle.Width = 0 Then
            rect.Width += 1
        End If

        If Me.ClientRectangle.Height = 0 Then
            rect.Height += 1
        End If

        If Me._GradientMode = LinearGradientMode.None Then
            filler = New System.Drawing.Drawing2D.LinearGradientBrush(rect, Me._BackColour1, Me._BackColour1, System.Drawing.Drawing2D.LinearGradientMode.Vertical)
        Else
            filler = New System.Drawing.Drawing2D.LinearGradientBrush(rect, Me._BackColour1, Me._BackColour2, CType(Me._GradientMode, System.Drawing.Drawing2D.LinearGradientMode))
        End If

        e.Graphics.FillPath(filler, graphPath)
        filler.Dispose()

        Select Case Me._BorderStyle
            Case System.Windows.Forms.BorderStyle.FixedSingle
                Dim borderPen As New System.Drawing.Pen(Me._BorderColour, Me._BorderWidth)
                e.Graphics.DrawPath(borderPen, graphPath)
                borderPen.Dispose()

            Case System.Windows.Forms.BorderStyle.Fixed3D
                SureDataPanel.DrawBorder3D(e.Graphics, Me.ClientRectangle)

            Case System.Windows.Forms.BorderStyle.None

        End Select

        filler.Dispose()
        graphPath.Dispose()


    End Sub
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 40325910
I am in a hurry and do not have the time to write the code or retrieve it from my old application.

Simply create a class level variable to store the Graphics object when entering the OnPaintBackground. Class level variables are visible everywhere in the class, so you will be able to use it in the Tick event.
0
 

Author Closing Comment

by:fmichail
ID: 40326486
Thank you very much JamesBurger. Good multiple ideas.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
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…

733 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