Wednesday, December 23, 2009

Form_Load Event vs overriding onLoad

I'm still using Windows Forms with C# (yep I'm the one.)

I've been having this nasty little form painting problem on inherited forms, whereby as the form loads, it does stuff in the load events of the child, parent and in some cases "grand parent" form.

The painting is ugly because as the form sizes and positions to it's last state, done in the very base form (top of th hierarchy) it repaints the form by not the controls within it yet.

After trying heaps of stuff including refreshing, invalidating, pausingLayout etc etc, I discovered a post someone else with a very similar problem.

Turns out the overriding the OnLoad event gives you much better control in this scenario because by calling base.OnLoad at the start of your override, you let Windows get all it's stuff done (and probably the child form "stuff" as well) then get back to your resizing code - which makes this all a lot smoother...

...anyway, mental note - override onLoad instead of using the form_load event wired up by the designer.

Thanks to this post!