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
341 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

630 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