views:

65

answers:

5

I have three classes derived from Control...Class1, Class2 and Class3.

Class1 is the parent. It contains a list of Class2 objects, which, in turn, each contain a list of Class3 objects.

Each class overrides the OnPaint method to paint itself.

Class1 and Class2 paint themselves fine but Class3.OnPaint is never called.

Do I need to do anything special (perhaps in Class2.OnPaint) to ensure that Class3 objects get invalidated and receive the Paint message?

+1  A: 

OnPaint will only get called if there is a visible region that has been invalidated (either because it became un-obscured or because it was manually invalidated.)

Can you post some simplified snippets of code that exhibit the problem?

Josh Einstein
I worked out the problem. The calculated co-ordinates for Class3's Location was being set with screen co-ordinates not relative to its parent control (Class2)...being co-ordinates larger than the size of their parents, they were effectively always off-screen.Thanks for your help!
Sambo
A: 

By "contains as list" do you mean that (1) the child controls have been added to .Controls on the parent control, or (2) that you have a list of controls as a variable within the parent control?

If (1) is the case, you can force a paint of the parent control and all child controls by calling .Refresh() on the parent control. Calling .Invalidate() will not necessarily mean that the control or its children will be painted right away.

If (2) is the case, you should add the controls to .Controls on the parent control to ensure they will be painted.

Zach Johnson
A: 

Try this in the constructor of the control.

this.SetStyle(ControlStyles.UserPaint, true);
sadboy
The Control base class already sets this.
Josh Einstein
A: 

I think the Controls has to be chained together using the Controls collection in each of them, so any parent control should have the child control(s) added to its Controls collection which I think how the OnPaint event gets populated.

bashmohandes
A: 

My bad! I wasn't setting the Location of Class3 controls relative to their parent control and, being larger values than the Size of the parent control, they were always off-screen.

Thanks for your help!

Sambo