Visual-studio – How to tell if .NET code is being run by Visual Studio designer

gui-designervisual studio

I am getting some errors thrown in my code when I open a Windows Forms form in Visual Studio's designer. I would like to branch in my code and perform a different initialization if the form is being opened by designer than if it is being run for real.

How can I determine at run-time if the code is being executed as part of designer opening the form?

Best Answer

To find out if you're in "design mode":

  • Windows Forms components (and controls) have a DesignMode property.
  • Windows Presentation Foundation controls should use the IsInDesignMode attached property.