I've been searching all morning and unfortunately I'm not sure what the techincal term is for this issue, so I'm unable to find a resolution.
When I derive from a GroupBox and override the onPaint function the groupboxes are redrawing themselves over-top the previous groupboxes. The child controls paint correctly, just the groupbox is affected..
class ExtendedComponents
{
public partial class extendedGroupBox : GroupBox
{
private Color borderColor;
public extendedGroupBox()
{
this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ContainerControl, true);
this.borderColor = Color.Black;
}
[NotifyParentProperty(true)]
public Color BorderColor
{
get { return this.borderColor; }
set { this.borderColor = value; Invalidate(); }
}
protected override void OnPaint(PaintEventArgs e)
{
Size tSize = TextRenderer.MeasureText(this.Text, this.Font);
Rectangle borderRect = e.ClipRectangle;
borderRect.Y += tSize.Height / 2;
borderRect.Height -= tSize.Height / 2;
ControlPaint.DrawBorder(e.Graphics, borderRect, this.borderColor, ButtonBorderStyle.Dotted);
Rectangle textRect = e.ClipRectangle;
textRect.X += 6;
textRect.Width = tSize.Width + 5;
textRect.Height = tSize.Height;
e.Graphics.FillRectangle(new SolidBrush(this.BackColor), textRect);
e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), textRect);
}
}
}
Any help would be much appreciated!
The easy answer is to not use the GroupBox control-- it's inherently flicky.
Try using a Panel control instead with your DoubleBuffer SetStyles, etc.
For your current implementation, don't use the e.ClipRectangle
:
//Rectangle borderRect = e.ClipRectangle;
Rectangle borderRect = this.ClientRectangle;
//Rectangle textRect = e.ClipRectangle;
Rectangle textRect = this.ClientRectangle;