I think I missed something trivial here. I got a simple control directly from Control . I override OnPaint and draw a rectangle ( e.Graphics.DrawRectangle ) and the text inside it ( e.Graphics.DrawString ). I did not redefine the other members.
It worked well when the control was resized to a smaller size, but when it gets larger, the new area does not repaint properly. As soon as I resize it to a smaller size again, even if it is one pixel, everything will display correctly.
OnPaint is called correctly (with the corresponding PaintEventArgs.ClipRectangle set correctly to the new area), but the new area is not painted (artifacts appear) in any case.
What am I missing?
EDIT:
code:
protected override void OnPaint(PaintEventArgs e) { // Adjust control height based on current width, to fit current text: base.Height = _GetFittingHeight(e.Graphics, base.Width); // Draw frame (if available): if (FrameThickness != 0) { e.Graphics.DrawRectangle(new Pen(FrameColor, FrameThickness), FrameThickness / 2, FrameThickness / 2, base.Width - FrameThickness, base.Height - FrameThickness); } // Draw string: e.Graphics.DrawString(base.Text, base.Font, new SolidBrush(base.ForeColor), new RectangleF(0, 0, base.Width, base.Height)); } private int _GetFittingHeight(Graphics graphics, int width) { return (int)Math.Ceiling(graphics.MeasureString(base.Text, base.Font, width).Height); }

source share