views:

819

answers:

2

Which is a usercontrol last event to fire when the application/control is loaded? The load event?

A: 

Winforms controls don't have a lifecycle in the same sense that asp.net webforms controls do. UI events happen when the user does something to make them happen.

Joel Coehoorn
+4  A: 

That really depends on when during its life you are talking about. Given your guess at Load, we talk during creation/load. This list shows a few events in the order that they were fired in a simple UserControl that I set up for testing (hooked up a number of eventlisteners and printed the event name to the console):

HandleCreated
Load
Layout
VisibleChanged
Paint

Note that Layout, VisibleChanged and Paint does not strictly have anything to do with the control creation; Layout and in particular Paint may or will be raised at a number of occasions during the life of the control. The same goes for VisibleChanged which is raised whenever the value of the Visible property is changed.

Fredrik Mörk
+1 - you beat me to it. I got the same result - Paint is last. Also note that CreateControl happens between HandleCreated and Load, and PaintBackground happens before Paint.
Jon B
@Jon B: there is no PaintBackground event, but there is the protected OnPaintBackground method (that looks very much like your standard event raising method, but that does not raise any event). Either way, CreateControl and OnPaintBackground can be used only inside the user control, not from external code (such as the form hosting the control).
Fredrik Mörk
@Jon B: forgot to mention; good points with those two methods still! :)
Fredrik Mörk