BeginUpdate() EndUpdate for a 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.
精彩评论