Question

I have winforms control that can draw complex UI in OnPaint method. In each property of control Invalidate method is called.

public new Color BackColor
    {
        get
        {
            return backColor;
        }
        set
        {
            backColor= value;
            this.Invalidate();
        }
    }

Is it more effective to call Invalidate method only once in case if few properties are changed?

For example:

    // remove from properties Invalidate call and call it once after set:
    this.BackColor=Red; 
    this.ForeColor=Blue; 
    this.BorderColor=Gray;

    this.Invalidate();
Was it helpful?

Solution

It is not really more effective to call Invalidate only once because once you invalidate the form, extra calls to Invalidate are ignored until you Paint the window. (it is more efficient not to call Invalidate unnecessarily, but the cost for unnecessary calls is very low)

If you want to make things more efficient, look at how you can make your Paint handler less complex. Also, if only part of the display needs to be redrawn, you may be able to Invalidate only part of the display area, and your Paint handler can only draw the content that is visible within its clip rectangle, which can significantly reduce the work you do to update the screen when a property is changed.

OTHER TIPS

you only need to call invalidate() once.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top