Click here to Skip to main content
15,912,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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. Why is this happening and how to get rid of it?
My code follows:

VB
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
Posted
Updated 7-Dec-11 5:11am
v2

1 solution

I would try to add System.Windows.Forms.ControlStyles AllPaintingInWmPaint | OptimizedDoubleBuffer using System.Windows.Forms.Control.Control.SetStyle, see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.setstyle.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.setstyle.aspx[^].

Also, I would derive your class from Control and did not call base OnPaint (and paint the background yourself), as you don't need any functionality specific to Panel.

However, I don't see anything fundamentally wrong in your code; I would even suspect that something is wrong with your system. I remember I had some Forms artifacts showing on Windows 2000 but never on XP. So, I would suggest you try your code on different systems, just in case.

—SA
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Dec-11 10:37am    
OP commented:

Thanks SAKryukov for your help. Here's another info I dug up. The reason my code isn't working is I draw border for e.ClipRectangle, which may be just a small part of the control that has been invalidated for current paint event. I need to use the control's ClientRectangle property instead of ClipRectangle. For more advanced painting the clip rectangle can be used to only redraw the invalidated area.
Sergey Alexandrovich Kryukov 13-Dec-11 10:39am    
Thank you for reporting it back and accepting the answer.

However, please don't post anything as a solution unless you really want to post a solution. Such posts will be removed, no one gets e-mail notification. Instead, add comment via "Have a Question of Comment?" (below), add a reply to existing comment, use "Improve question" above.

--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900