CodeCamper
CodeCamper

Reputation: 6980

Overriding OnPaintBackground makes the Designer view misbehave

I have to disable the OnPaintBackground on my TableLayoutPanel to remove flickering caused from the background being drawn first(because I am drawing on the TLP with the paint method, and yes I need a TLP because it contains many controls for a purpose). So my code is as follows:

    public static bool FlickerPanel = false;
    public class FlickerTableLayoutPanel : TableLayoutPanel
    {
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            if (FlickerPanel)
                base.OnPaintBackground(e);
        }
    }

Then in my paint method I have it draw it's own background. So during runtime it is fine.

Edit: I discovered the root of the problem. By overriding the OnPaintBackground it disables whatever code is making the designer draw the background. If I remove the override all together it doesn't have the graphical glitch.

        protected override void OnPaintBackground(PaintEventArgs e)
        {
                base.OnPaintBackground(e);
        }

Even this above code disabled the Design view rendering and causes graphical glitches. Any help much appreciated!

Upvotes: 0

Views: 5507

Answers (4)

pjs
pjs

Reputation: 2731

I just wanted to contribute my slight upgrade to the solution that @matthew-watson provided. As an extension method, the syntax becomes a bit more pleasing to read.

public static partial class ExtensionMethods
{
    public static bool IsInDesignMode(this Control source)
    {
        var result = LicenseManager.UsageMode == LicenseUsageMode.Designtime;

        while (result == false && source != null)
        {
            result = source.Site != null && source.Site.DesignMode;

            source = source.Parent;
        }

        return result;
    }
}

and used like this

protected override void OnPaintBackground(PaintEventArgs pevent)
{
    if (this.IsInDesignMode())
    {
        base.OnPaintBackground(pevent);
    }
}

Upvotes: 1

Faruk
Faruk

Reputation: 5831

just do nothing onPaintBackground() function. Its prevent backgroundImage to be drawn and should fix the flickering.

protected override void OnPaintBackground(PaintEventArgs e)
{
}

Upvotes: 0

Matthew Watson
Matthew Watson

Reputation: 109587

I also had problems detecting whether my form was in design mode. I solved it as follows:

Firstly, I had to write an IsDesignMode() method:

public static bool IsDesignMode(Control control)
{
    if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)  // Initial quick check.
    {
        return true;
    }

    while (control != null)
    {
        System.ComponentModel.ISite site = control.Site;

        if ((site != null) && (site.DesignMode))
            return true;

        control = control.Parent;
    }

    return false;
}

I put that method in a shared library assembly (namespace "Windows.Forms.Utilities" in the example below), since I use it in a lot of projects.

Then for each user or custom control where I need to know if it's in design mode, I have to add a private bool _isDesignMode field and override OnHandleCreated() as follows:

protected override void OnHandleCreated(EventArgs e)
{
    base.OnHandleCreated(e);
    _isDesignMode = Windows.Forms.Utilities.IsDesignMode(this);
}

Then I can use _isDesignMode wherever I need to.

Upvotes: 4

Romano Zumbé
Romano Zumbé

Reputation: 8079

You could surround the code in the OnPaintBackground method with an if statement to detect if your in design time like this:

if (System.ComponentModel.LicenseManager.UsageMode ==
System.ComponentModel.LicenseUsageMode.Designtime)

Upvotes: 1

Related Questions