Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
348 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

670 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