CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2008
    Posts
    90

    Question Problem in painting custom panel control (.Net Framework 4.0).

    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:

    Code:
    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
    Attached Images Attached Images    

  2. #2
    Join Date
    Sep 2008
    Posts
    90

    Thumbs up Re: Problem in painting custom panel control (.Net Framework 4.0).

    The following line needs to be changed:

    Code:
    ControlPaint.DrawBorder(e.Graphics, Me.ClientRectangle, BorderColor, borderWidth, ButtonBorderStyle.Solid, BorderColor, borderWidth, ButtonBorderStyle.Solid, BorderColor, _
                    borderWidth, ButtonBorderStyle.Solid, BorderColor, borderWidth, ButtonBorderStyle.Solid)
    The reason 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. The control's ClientRectangle property needs to be used. For more advanced painting the clip rectangle can be used to only redraw the invalidated area.

  3. #3
    Join Date
    Sep 2012
    Posts
    1

    Re: Problem in painting custom panel control (.Net Framework 4.0).

    i have the same problem: when i want to resize my form with my custom drawn panel it does not refresh the border at all, even when i use me.clientrectangle instead of dim rct as new rectangle(0,0,me.width,me.height)
    Attached Files Attached Files

  4. #4
    Join Date
    Sep 2008
    Posts
    90

    Re: Problem in painting custom panel control (.Net Framework 4.0).

    According to the attachment you posted, just add Me.Invalidate() to the RPanel_Resize event handler of UserControl1. Hope that sorts it out.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured