Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnPaint override is never called

Tags:

c#

winforms

I have been at this for a few days and it is driving me mad. I have a control that inherits from System.Windows.Forms.Panel and I'm trying to override OnPaint. It just plain, outright IGNORES it.

public class CollapsiblePanel : System.Windows.Forms.Panel
{
  public CollapsiblePanel()
  {
   //
   // Required for the Windows Form Designer
   //
   InitializeComponent();

   //
   // TODO: Add any constructor code after InitializeComponent call
   //
   SetStyle
    (
     ControlStyles.AllPaintingInWmPaint | 
     ControlStyles.UserPaint      | ControlStyles.DoubleBuffer   |
     ControlStyles.ResizeRedraw     | ControlStyles.Selectable ,
     true
    );
        }

  protected override void OnPaint(PaintEventArgs e)
  {
            // This never runs no matter what I try!
            base.OnPaint(e);
        }
}
like image 600
Ciel Avatar asked Feb 07 '10 00:02

Ciel


1 Answers

I have the same problem just with a ProgressBar, when i try to override the OnPaint.. It's never called.


I found the solution here: http://web.archive.org/web/20140214234801/http://osix.net/modules/article/?id=826

You must create a constructor and enable user-painting like this:

SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);

Default values, probably vary depending on framework version and OS.

like image 161
Joe Avatar answered Sep 22 '22 23:09

Joe