Visual Studio 2005 and the Load method of UserControl

Posted by on Comments (0)

Maybe you already met this kind of problems with UserControl and Visual Studio 2005 : you design a main winform without any problems, you design a user control without any problem. But then when you add the user control to the main form, the designer will encouter an error on the main form. You nevertheless may run the application without any problem.

The reason for this is that the designer is calling the load method of user controls when you open the form that is hosting those user controls.

 

A simple test :

Here is the designer part of my user control (an empty one with just one label, this is enough to demonstrate our problem):


Here is the code behind :



The important thing there is the Load method.

 

Then, if I want to add the user control to a main empty form, here is what you will see :

Most of the time, this is not annoying, but sometimes, this may lead to the impossibility to woon the main form with the designer (because you are in design time and not run time, information may not be available):

 


You can then work with the DesignTime property of the user control :

 

Posted in: .NET 2.0 | Visual Studio 2005   Tags: