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

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you very much JamesBurger. Good multiple ideas.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now