title: Winforms groupbox with colored border
body: <p>I have used the following code to create a groupbox with colored borders:</p>
  
  <pre><code>Public Class BorderGroupBox
      Inherits GroupBox
  
      Private _borderColor As Color
      Private _borderWidth As Integer
      Private _borderStyle As ButtonBorderStyle
  
      ...
  
      Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
          Dim tSize As Size = TextRenderer.MeasureText(Me.Text, Me.Font)
          Dim borderRect As Rectangle = e.ClipRectangle
          borderRect.Y = CInt((borderRect.Y + (tSize.Height / 2)))
          borderRect.Height = CInt((borderRect.Height - (tSize.Height / 2)))
          ControlPaint.DrawBorder(e.Graphics, borderRect, _borderColor, _borderWidth, _borderStyle, BorderColor, _borderWidth, _borderStyle, BorderColor, _borderWidth, _borderStyle, BorderColor, _borderWidth, _borderStyle)
          Dim textRect As Rectangle = e.ClipRectangle
          textRect.X = (textRect.X + 6)
          textRect.Width = tSize.Width + 6
          textRect.Height = tSize.Height
          e.Graphics.FillRectangle(New SolidBrush(Me.BackColor), textRect)
          e.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(Me.ForeColor), textRect)
      End Sub
  End Class
  </code></pre>
  
  <p><strong>The problem is, it is placed inside a scrollable container, and if it is scrolled the border isn't redrawn correctly:</strong></p>
  
  <p><img src="https://i.stack.imgur.com/tenCP.jpg" alt="Redraw"></p>