C# .NET - Getting OnPaint Called in Contained Control

Asked By Patrick Skelton on 16-Sep-08 09:31 AM
Hi,

I have a control (inherited from the System.Windows.Forms.TabPage), to which I have added another custom control, by calling Controls.Add( myNewSubControl ).

My problem is that my overriden OnPaint() in my subcontrol never gets called.  Do I need to do anything special to make this be called?

Any help would be most appreciated.

Best wishes, Patrick



solution - Perry replied to Patrick Skelton on 16-Sep-08 10:31 AM

Add follwoing linke to your derived constructor:

this.SetStyle(ControlStyles.UserPaint, true);

It will solve your problem.

-Paresh

solution - Perry replied to Patrick Skelton on 16-Sep-08 10:34 AM

Take care to "Call this.Invalidate()" at the end of your OnPaint override method.

-Paresh

No Luck - Patrick Skelton replied to Perry on 16-Sep-08 10:37 AM

Hi,

Thank you for the reply.

Unfortunately, putting that into both the containing tab page and also the contained sub control doesn't seem to have any effect - the OnPaint() is still not being called.

-Patrick


Already being called - Patrick Skelton replied to Perry on 16-Sep-08 10:43 AM
Hi,

Invalidate( true ) for the containing tab page is already been called on a timer.

I must be doing something stupid somewhere. :-(

-Patrick


solution - Perry replied to Patrick Skelton on 16-Sep-08 10:49 AM

I hope that in your class, you typed protected override.

-Paresh

solution suggestions - Perry replied to Patrick Skelton on 16-Sep-08 10:52 AM

Hi,

OnPaint is a protected member of Control classes.  It is called by the class when it wants to raise the Paint event, which indirectly means when it wants to paint.

If you are creating a new control class, the recommended method of overriding the paint for that class is to override OnPaint perform you painting within OnPaint, optionally calling the base.OnPaint, or optionally raising the Paint event.  See Control.OnPaint in MSDN for more information.

If you're not creating a new class, you can only subscribe to the Paint event for a control and perform painting operations when you receive the paint event.

OnPaint() function is only allowed to be called from within that Panel class (source code that only Microsoft has) OR a class that you write that derives from the Panel class. If you have not created panel class then please do it.

-Paresh

solution - Perry replied to Patrick Skelton on 16-Sep-08 10:54 AM

I see, please also inherit "public System.Windows.Forms.Panel" from your class along with System.Windows.Forms.TabPage.

-Paresh

Yes - Patrick Skelton replied to Perry on 16-Sep-08 10:55 AM
Yes, Paresh - both my OnPaint() handlers are overrides, and both call the base version before drawing my extra things.

-Patrick


Sorry - Patrick Skelton replied to Perry on 16-Sep-08 10:57 AM
I don't understand what you mean.  You mean inherit from Panel instead of TabPage?

-Patrick
both - Perry replied to Patrick Skelton on 16-Sep-08 11:03 AM

Inherit both. I think Panel class is mandatory for calling OnPaint() method.

-PAresh

paste your code here - Perry replied to Patrick Skelton on 16-Sep-08 11:04 AM

Could you please paste your code here?

-PAresh

TabPage Already Inherits from Panel - Patrick Skelton replied to Perry on 16-Sep-08 11:10 AM
Hi, Paresh,

I have just checked, and it seems that the TabPage control already inherits from Panel.

I will try to change what my sub-control derives from.  At the moment, it derives directly from Control.

-Patrick


override onpaint - Partha Mandayam replied to Patrick Skelton on 16-Sep-08 11:27 AM
Here's an example of overriding the onpaint method.

http://msdn.microsoft.com/en-us/library/cksxshce.aspx
sethuram replied to Partha Mandayam on 28-Jul-10 04:58 AM
 try

this.ownerdraw=true