Wednesday, August 19, 2009

 

Form.Load() override never called

I came across an issue today that boggled my mind for a few minutes. I had an apparently normal WinForm's Form, but the OnLoad() override was never getting called. Therefore, the Load event was never rasied.

As it turns out, there was nothing wrong with the form itself, but it was being used in an unconventional way. For some special-case reasons, the code that caused the form to be displayed was using PInvoke calls to SetWindowsPos() and ShowWindow() instead of calling the Form.Show() or Form.ShowDialog() method. Apparently, because neither Show() nor ShowDialog() was used, the OnLoad() method was never called.

My solution to this problem was to override the OnVisibleChanged() method, use a flag field to track whether the initialization code had been executed, and call the initialization code from whithin OnVisibleChanged if it had not yet been executed and Visble just changed to true. The flag, of course was then changed to true so the initialization code would not be called again if the form went invisible and then became visible again.

Comments: Post a Comment





<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]