Question

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..

Screenshot

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!

Was it helpful?

Solution

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;

OTHER TIPS

Another thing to note is that you should override OnPaintBackground to avoid flicker. There you either do nothing or draw the control fore color.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top