getting OnPaint to fire

R

Ron Vecchi

I have a custyom control that has a property which wraps a collection. When
I add controls to the collection the OnPaint method of these newly added
controls never get fired.
But if I take the controls in the collection and add them to the custom
controls ControlCollection the OnPaint method is called.

I would like to use my Collection property but It seems that the controls
need to be added to the ControlCollection for them to work.
In my custom controls on paint method I loop through the Collection and add
them to the ControlCollection so the OnPaint will be called.

I'm pretty new to this, just wondering if this is an ass backwards way to
do this. Seems like it is and that I am missing something.
--------------------------------------

// myothercontrol OnPaint is never called from below example
MyCustomControl.CollectionPreoperty.Add(myothercontrol);

------------------------------------------------
////
// MyCustomControl OnPaint Method
// OnPaint menthod will get fired by using below example.
//

protected void OnPaint(PaintEventArgs pe){
foreach(MyCustomControl in this.CollectionProperty){
this.Controls.Add(MyCustomControl);
}
}


--------------------------------
I was thinking that in the set and get accessor for the Collection Property
I could just get and set directly to the ControlCollection
but it still seems like I am wrong in my organization and thinking on this.


Any Insights?
 
J

Jeffrey Tan[MSFT]

Hi Ron,

Thanks for posting in this group.
Based on my understanding, you find that the only the custom control's
Controls.Add method can make the child controls OnPaint method gets called.
I think this is by the design of .Net, Control.Controls.Add method does
many internal works.
So I think you should use the default ControlCollection of your custom
control.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
N

Nicholas Paldino [.NET/C# MVP]

Jeffrey,

I am curious, couldn't the original poster just call Invalidate on the
parent hosting the controls, which will in turn cause the parent and the
child controls (including the ones just added) to repaint correctly?
 
J

Jeffrey Tan[MSFT]

Hi Nicholas,

Yes, Invalidate method will explicit force the control to repaint. So you
can also do this instead.
But I think the control's default controlcollection property is more
convinient to use. And it should meet your need.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
C

Casey Leamon

As long as the custom control's onPaint is properly overriden, Invalidate()
should do exactly what you're looking for Ron.
Nicholas Paldino said:
Jeffrey,

I am curious, couldn't the original poster just call Invalidate on the
parent hosting the controls, which will in turn cause the parent and the
child controls (including the ones just added) to repaint correctly?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

"Jeffrey Tan[MSFT]" said:
Hi Ron,

Thanks for posting in this group.
Based on my understanding, you find that the only the custom control's
Controls.Add method can make the child controls OnPaint method gets called.
I think this is by the design of .Net, Control.Controls.Add method does
many internal works.
So I think you should use the default ControlCollection of your custom
control.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top