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
315 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Vb.net dynamic formulas in runtime 11 75
Convert an ASPX page into PDF 7 32
Creating a route in asp.net webforms 2 30
get column names from table in vb.net 8 30
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

831 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