多次执行OnPaintBackground C#?

多次执行OnPaintBackground C#?,c#,winforms,user-controls,panels,C#,Winforms,User Controls,Panels,我已经创建了一个用户控件,将屏幕分割成矩形,但是我的控件在paintbackground上执行了很多次OnPaintBackground,而它应该只执行一次。请帮助我,因为只执行一次非常重要,因为scrren启动了很多 public partial class Schedual : UserControl { int days; public int Days { get { return days; } set {

我已经创建了一个用户控件,将屏幕分割成矩形,但是我的控件在paintbackground上执行了很多次
OnPaintBackground
,而它应该只执行一次。请帮助我,因为只执行一次非常重要,因为scrren启动了很多

public partial class Schedual : UserControl
{
    int days;

    public int Days
    {
        get { return days; }
        set
        {
            days = value;
            change = true;
            Invalidate(true);
        }
    }

    int periods;

    public int Periods
    {
        get { return periods; }
        set
        {
            periods = value;
            change = true;
            Invalidate(true);
        }
    }

    Brush brush;

    bool change = false;

    List<Panel> panels;

    public Schedual()
    {
        InitializeComponent();
        this.ResumeLayout(true);
        this.days = 1;
        this.periods = 1;
        brush = Brushes.White;
        change = false;
     }

    protected override void OnPaint(PaintEventArgs e)
    {
        //stuff ....... or base.OnPaint(e);
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        var h = this.Height / days;
        var w = this.Width / periods;
        for (int i = 0; i < days; i++)
        {
            for (int j = 0; j <= periods; j++)
            {
                g.FillRectangle(brush, j * w, i * h, w, h);
                if (change)
                {
                    AddPanel(j * w, i * h, w, h);
                }
                g.DrawLine(Pens.Black, 0, i * h, this.Right, i * h); //draw the horizantle lines
                g.DrawLine(Pens.Black, j * w, 0, this.Bottom, j * w); //draw the verical lines
            }
        }
        change = false;
    }

}
公共部分类Schedual:UserControl
{
国际日;
公众整数日
{
获取{返回天数;}
设置
{
天数=价值;
改变=正确;
无效(真);
}
}
整数周期;
公共整数周期
{
获取{返回周期;}
设置
{
周期=价值;
改变=正确;
无效(真);
}
}
刷子;
布尔变化=假;
名单小组;
公共服务
{
初始化组件();
此.resume布局(true);
这1.5天=1;
这个周期=1;
刷子=刷子。白色;
改变=错误;
}
受保护的覆盖无效OnPaint(PaintEventArgs e)
{
//涂料(e)上的填充物或基底;
}
PaintBackground上受保护的覆盖无效(PaintEventArgs e)
{
图形g=e.图形;
var h=此高度/天;
var w=此宽度/周期;
对于(int i=0;i

请参阅此处的详细信息-

使用双缓冲。@SLaks您能指出如何执行此操作吗