SCENARIO
In WinForms, I've sub-classed a GroupBox
to change the border color of this control.
PROBLEM
At design mode (in the visual builder of VisualStudio), if I perform any kind of changes to the controls inside my Groupbox
lets say click on each control to change the textfont then my Groupbox
redraws the controls like this:
Note: After invalidating the control it redraws again properly.
QUESTION
This is a known issue when owner-drawing a container that stores a control collection like a GroupBox
?
What I'm missing to do in the OnPaint
method to fix this painting issue?
CODE
VB Version:
''' <summary>
''' Handles the <see cref="E:Paint"/> event.
''' </summary>
''' <param name="e">A <see cref="T:PaintEventArgs"/> that contains the event data.</param>
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
' MyBase.OnPaint(e)
Me.DrawBorder(e)
End Sub
''' <summary>
''' Draws a border on the control surface.
''' </summary>
Private Sub DrawBorder(ByVal e As PaintEventArgs)
' The groupbox header text size.
Dim textSize As Size = TextRenderer.MeasureText(Me.Text, Me.Font)
' The width of the blankspace drawn at the right side of the text.
Dim blankWidthSpace As Integer = 3
' The thex horizontal offset.
Dim textOffset As Integer = 7
' The rectangle where to draw the border.
Dim borderRect As Rectangle = e.ClipRectangle
With borderRect
.Y = .Y + (textSize.Height \ 2)
.Height = .Height - (textSize.Height \ 2)
End With
' The rectangle where to draw the header text.
Dim textRect As Rectangle = e.ClipRectangle
With textRect
.X = .X + textOffset
.Width = (textSize.Width - blankWidthSpace)
.Height = textSize.Height
End With
' Draw the border.
ControlPaint.DrawBorder(e.Graphics, borderRect, Me.borderColor1, Me.borderStyle1)
' Fill the text rectangle.
e.Graphics.FillRectangle(New SolidBrush(Me.BackColor), textRect)
' Draw the text on the text rectangle.
textRect.Width = textSize.Width + (blankWidthSpace * 2) ' Fix the right side space.
e.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(Me.ForeColor), textRect)
End Sub
C# version:
/// <summary>
/// Handles the <see cref="E:Paint"/> event.
/// </summary>
/// <param name="e">A <see cref="T:PaintEventArgs"/> that contains the event data.</param>
protected override void OnPaint(PaintEventArgs e)
{
// MyBase.OnPaint(e)
this.DrawBorder(e);
/// <summary>
/// Draws a border on the control surface.
/// </summary>
private void DrawBorder(PaintEventArgs e)
{
// The groupbox header text size.
Size textSize = TextRenderer.MeasureText(this.Text, this.Font);
// The width of the blankspace drawn at the right side of the text.
int blankWidthSpace = 3;
// The thex horizontal offset.
int textOffset = 7;
// The rectangle where to draw the border.
Rectangle borderRect = e.ClipRectangle;
var _with1 = borderRect;
_with1.Y = _with1.Y + (textSize.Height / 2);
_with1.Height = _with1.Height - (textSize.Height / 2);
// The rectangle where to draw the header text.
Rectangle textRect = e.ClipRectangle;
var _with2 = textRect;
_with2.X = _with2.X + textOffset;
_with2.Width = (textSize.Width - blankWidthSpace);
_with2.Height = textSize.Height;
// Draw the border.
ControlPaint.DrawBorder(e.Graphics, borderRect, this.borderColor1, this.borderStyle1);
// Fill the text rectangle.
e.Graphics.FillRectangle(new SolidBrush(this.BackColor), textRect);
// Draw the text on the text rectangle.
textRect.Width = textSize.Width + (blankWidthSpace * 2);
// Fix the right side space.
e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), textRect);
}
//=======================================================
//Service provided by Telerik (www.telerik.com)
//=======================================================