Friday, March 03, 2006

Inheriting Windows Forms in VS.Net 2003/2005 Problems

I recently had my first production use of inheriting windows forms in a C# Windows Application. For basic forms (where the base form has little/no logic) this is usually a snap. But I ran into problems when trying to open a derived form in VS.Net's design view. I would get a message like 'An error occurred while loading form' and/or 'Security Error'. It turns out that VS.Net not only creates an instance of your base form class during Design view of the derived Form, but also Loads the base form, so your Form_Load gets called. You may not intend any of this code to get executed during Design/Development. If you have any MessageBox calls, or any other code that assumes other code has executed first, you will run into problems, and the errors from the VS.Net IDE or not helpful. When running into design view problems with derived forms, start with your initialize and Form_Load methods in your base form. Good luck!