I have a function that causes an exception in the designer. Can I avoid the call of the function if the designer loads it. Is there an attribute or something that is better than try catch?
Some more details:
I mean the visual studio designer for win forms. My form is using a signleton wich calls LoadProject() on initialize. Now I want to avoid that the designer calls the LoadProject() function.
There are a few ways of detecting whether or not you are in design mode:
devenv.exeusingApplication.ExecutablePath.ToLower().IndexOf("devenv.exe"). If is does, the control is being instantiated by Visual Studio. A bit horrible, but it works.LicenseManager.UsageModefor the valueLicenseUsageMode.Designtime(have a look at my answer to Detecting design mode from a Control’s constructor for more details). Note that this does work in the constructor.Wrapping the call to your function in any of these checks should solve your problem.