I have inherited the standard panel control to create a panel having custom border color. The problem is, while the panel is on a form and any other form is moved across it, it paints hapazard lines of the same color as the panel custom border color, all throughout background of the panel. I tried to Refresh or Invalidate the panel in its paint event but of no avail. The same is happening while designing in the design editor too. See attached image. Why is this happening and how to get rid of it?
My code follows:

Namespace CustomPanelControl
    Public Class CustomPanel
        Inherits Panel

        Public Sub New()
            MyBase.New()
        End Sub

        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            MyBase.OnPaint(e)

            'Set the backcolor of the panel according to the focus on it.
            If Me.ContainsFocus Then
                Me.BackColor = Color.Yellow
            Else
                Me.BackColor = Color.White
            End If

            'Draw the border of the panel.
            If Me.BorderStyle = BorderStyle.None Then
                Dim borderWidth As Integer = 1
                Dim BorderColor As Color = Color.Blue
                ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, BorderColor, borderWidth, ButtonBorderStyle.Solid, BorderColor, borderWidth, ButtonBorderStyle.Solid, BorderColor, _
                borderWidth, ButtonBorderStyle.Solid, BorderColor, borderWidth, ButtonBorderStyle.Solid)
            End If
        End Sub
    End Class
End Namespace
Attachments DirtyWhileDesign.jpg 22.88 KB DirtyWhileExecute.jpg 24.04 KB Normal.jpg 16.97 KB

Its the clip rectangle in control paint thats giving you the problem. Change the clip rectangle to Me.ClientRectangle.

When are moving another form over it its sending the part of the panel that needs to be refreshed. Drawing the border with the clip rectangle is drawing the border on the last place the form was over it.

Comments
Very Good.

Hmm. I didn't really know the classification between ClientRectangle, bounds and ClipRectangle. Right on. Thanks.

This question has already been answered. Start a new discussion instead.