开发者

BeginUpdate() EndUpdate for a UserControl

开发者 https://www.devze.com 2023-02-11 11:55 出处:网络
I have written a UserControl that behaves like a ContainerControl, but开发者_StackOverflow社区 is totally painted by WindowsForms (I inherit from UserControl)

I have written a UserControl that behaves like a ContainerControl, but开发者_StackOverflow社区 is totally painted by WindowsForms (I inherit from UserControl)

I would like to avoid painting the control while I'm filling it, so I would need to write something similar to BeginUpdate() - EndUpdate().

This is easy to do when the control is user-painted, but in this case I'm not sure about how to proceed.


You could make use of Suspend/Resume layout. e.g.

private void BeginUpdate()
{
  this.SuspendLayout();
  // Do paint events
  EndUpdate();
}

private void EndUpdate()
{
   this.ResumeLayout();
   // Raise an event if needed.
}

If you're interested in suspending the painting of a control and it's children, check out this SO question: Suspend Control and Children Painting


You could override the OnPaint method and only pass control back to the base.OnPaint() when satisfying a certain condition.

    private bool _doPaint = true;
    protected override void OnPaint(PaintEventArgs e)
    {
        if(_doPaint)
            base.OnPaint(e);
    }

Then handle setting the _doPaint variable to the appropriate value with Public methods or a property.

You might have to override OnPaintBackground() in a similar way, depending on your needs.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号