|

Introduction
This article explains how to tackle one of the most dreadful situations Windows Forms developers encounter during their daily work - WSODs.
Background
The term WSOD (White Screen of Darn) was set by Microsoft employees in an MSDN blog.
Using the code
Imagine the following scenario. One morning you arrive at your workplace, turn your monitor on, and then try to open a form in your application that builds and runs perfectly well, and instead of seeing your handcrafted form, you see a big frightening screen with the following title in red, and a big X near it:
One or more errors encountered while loading the designer. The errors are listed below. Some errors can be fixed by rebuilding your project, while others may require code changes.
As a pretty fresh GUI developer, but a pretty experienced software developer all-in-all, I was pretty astonished when I first encountered this phenomenon. My coworkers were not as surprised as I was, but still solving this sort of a problem never seemed to be trivial.
Explaining the problem
When the Visual Studio Designer views forms, it runs the constructor of your code. However, the code may behave in an unexpected way when run out of the application context. This may result in some exceptions thrown. The Visual Studio Designer does not like those exceptions, and may show a screen similar to this one, instead of the screen you really wanted to see:

About the example
My example code consists of a form called MainForm. This form contains a user control called SevenHolder. The SevenHolder user control has nothing but a text box, whose content is updated from a singleton. The singleton class holding this information, simply called Singleton, holds an integer, which seems always to be 7. However, another singleton class called Global, limits the creation of the Singleton instance only to the time when the application is running. The Global instance is created as the first thing in this program, in Program.cs, where its Running attribute is set to true. Since SevenHolder updates the text box in the constructor, but the constructor will only obtain the Singleton instance on runtime, an exception will be thrown when the code is run in design-time.
The solution
Finding this sort of problems may prove very difficult by only reviewing the code. However, an easy solution exists. By taking the following steps, you may find the exact location of the code causing the exception, thus making it possible to change it in a way that the Designer works well with it:
- Open a new Visual Studio instance.
- Open any source file. This is required so that Visual Studio lets you attach to a process.
- Attach the new Visual Studio instance to the first one. The Visual Studio process is called devenv.exe. You only need to attach to managed code. Set Visual Studio to break on Common Language Runtime exceptions - Thrown and User-handled.
- Close the problematic form, and reopen it.
- This should result in an exception caught inside your code in the second instance of Visual Studio!

In our case, the following code was causing the problem: this.labelSevenHolder.Text = sing.Information.ToString();
In order to fix it, two approaches may be taken. The first one is to check the value of sing before using it: public SevenHolder()
{
InitializeComponent();
Singleton sing = Singleton.Instance();
if (sing != null)
{
this.labelSevenHolder.Text = sing.Information.ToString();
}
}
The second approach is to check the value of the DesignMode property inherited from System.ComponentModel.Component to see whether the application is indeed running before doing anything useful. This check should always be performed after the call to InitializeComponent though, otherwise your control may not render correctly in design-time: public SevenHolder()
{
InitializeComponent();
if (this.DesignMode)
return;
Singleton sing = Singleton.Instance();
this.labelSevenHolder.Text = sing.Information.ToString();
}
And of course, the complete code should look like this: public SevenHolder()
{
InitializeComponent();
if (this.DesignMode)
return;
Singleton sing = Singleton.Instance();
if (sing != null)
{
this.labelSevenHolder.Text = sing.Information.ToString();
}
}
- After recompiling the code, reopening
MainForm should result with the form. In this case, it does not look exactly in design-time as the form in run-time, because, of course, the content of the label inside the user control is only known to the application in run-time.

Points of Interest
Investigating this sort of problems really makes one understand how the Visual Studio designer works under-the-hood.
History
- Minor fixes: 31/3/2006.
- Initial version: 28/3/2006.
| You must Sign In to use this message board. |
|
| | Msgs 1 to 25 of 31 (Total in Forum: 31) (Refresh) | FirstPrevNext |
|
|
 |
|
|
Hi ,
when am trying to open designer view for a form which is inheriting BaseViewForm getting white screen with this message:
One or more errors encountered while loading the designer. The errors are listed below. Some errors can be fixed by rebuilding your project, while others may require code changes.
Could not load file or assembly 'Wrappers.CLI, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified. Hide
at MYnamespace.BaseViewForm..ctor()
Observatrions: 1. In C# project I added a reference to Wrappers.CLI.dll( This Project VC++ project Wrapper one which makes contact from C# code to c++ code) Wrappers.CLI version is 1.0.0.1 but when I added reference to this the VS shows the version as 0.0.0.0 (By looking at properties after adding reference) So suspected the variables from Wrappers.CLI.dll which declared in C# form (which is being opened in design view) may cause the error.
2. I created another BaseViewFormNew without having code related to Wrappers.CLI.dll and made to use this Base form instead of earlier BaseViewForm (which contains code related to Wrappers.CLI.dll then this forms gets opened. This approach worked fine. Then once anyof the forms design view is opened am reverting abck to inherit using older BAseViewForm then surprisingly the design view is getting opened . But this works in one machine in other machines this is not working. So what would be the solutions? No idea.
I tried
1. rebuilding, Clean and rebuilding, deleting bin,obj files 2. steps in http://www.codeproject.com/KB/cs/wsod.aspx 3. Deleting cache fiels from Project Assemblies folder
but none was working..
Thanks, Ramki
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi,
Am getting the problem while viewing the designer of the windows form (developed in C#)
C# project has references to wrapper classes(to contact with C++ classes).
Error :
One or more errors encountered while loading the designer. The errors are listed below. Some errors can be fixed by rebuilding your project, while others may require code changes.
Could not load file or assembly Wrappers.CLI, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified. Hide
at BaseClass..ctor()
The reference had been added through project browse folder only.
I tried using the steps given in the article but could not get the exact line of code where it was giving this exception.
Thanks, Ramki
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Use Debug.Assert either in a catch clause or as a conditional statement The assertion works even in design mode.
try { } catch(Exception err) { Debug.Assert(false,err.Message); }
or
Debug.Assert(text != null,"The text is noll"); Natza Mitzi
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi, thanks for good article!
But, there is littlebite mistake.
The body of DesignMode property looks like this:
protected bool DesignMode { get { bool result = false; ISite site = this.Site; if ( site != null ){ result = site.DesignMode; }
return result; } }
What it mean? The DesignMode property in constructor always returns False!
------
There is next tricky question.
If you have this classes:
MyTextBox // derives from TextBox
MyUserControl // derives from UserControl and contains one or more controls "MyTextBox"
MainForm // derives from Form and contains one or more controls "MyUserControl"
The Visual Studio Designer sets the Site property only to controls added in designed control/form, but not to controls inside. So, if you open the designer for MainForm the instances of MyUserControls will have the DesignMode set to True bud the instances of MyTextBox will have the DesignMode always set to False.
The solution is the helper class with simple method IsInDesignMode which looks like this:
public static class MyHelperClass { // Returns True, if specified control or one of their parent control is in design mode. public static bool IsInDesignMode( Control control ) { if ( control == null ) { throw new ArgumentNullException( "control" );
bool result = false; // return value Control ctl = control; // checked control for design mode do { ISite site = ctl.Site; // get the site object, which is set by designer if ( site != null ) { result = site.DesignMode; // check for design mode if ( result ) { break; } // if control is in design mode then loop ends } } while ( ( ctl = ctl.Parent ) != null ); // track the parent control
return result; } }
Then the using in the custom/user control should looks like this:
public class MyTextBox : TextBox { public MyTextBox() { ... // avoid any code which potentialy throws exception(s) }
protected override void OnLoad( EventArgs e ) { base.OnLoad( e );
if ( MyHelperClass.IsInDesignMode( this ) == false ) { // some code which potentialy throws exception(s) } } }
/* Geniality is in simplicity. */
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
It's worth mentioning that this behaviour is a 'feature' of VS2005, VS2003 didn't do it, and when reported as a bug, MS reported this was a new feature. I actually have forms that give the WSOD, and don't throw an exception when attaching a second instance, so it can go a little deeper, but your article will help a lot of people who are stuck on this Microsoft insanity.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Christian, I have in fact a form giving WSOD and not throwing exception when attaching a second instance. This is a VB project imported from VS 2003. What can I do? Thanks
Leonardo Presciuttini
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
First step is to look at your load event and start commenting things out, until you find the line that causes the problem. The DesignMode property can be used to make the designer skip lines.
I've posted on the MS forums about my problem and no-one from MS replied, so I was unable to solve my problem, I just live with it.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
In effect my routine at load event contains references to objects which are initialized outside of it. But putting 'exit sub' at the beginning has given no result: all runs as before. Moreover, other forms of mine, which have no load event routine give the same error. But in their case it is sufficient to display the associated resource file (after showing all project files) for the error to disappear. This is not sufficient for the form with load event. ANy
Leonardo Presciuttini
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
Hello,
I'm getting the same errors but for a different reson. I have a class that inherit an activex. now i made that class as a singleton because i want everybody to have an access to it. it is working fine but in design time i can't see the page were the control is in it. (I get an error) my initialization in the designer is like that:
myactivex = ClsActivex.Instance();
In the instance i make the new ClsActivex.
any idea whan can i do to avoid the error?! (i don't care that i wouldn't see the control in design time.
Regarts, Lin.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
I have a sub-class of UserControl that populates a TreeView from a database. This is done in the constructor. I use
if (this.DesignMode) return;
to avoid reading from the database in design mode since the connection information is not available at this time. This works fine when designing the UserControl.
However, when I add the UserControl to a Form I get the "WSOD". It seems that InDesignMode is only set to true when the UserControl itself is opened in the designer.
What I tried is to wrap the code that reads from the database in a try-catch block and ignore any Exception thrown. I do not understand why the Exception that is thrown within the try-block is not caught. Does anyone have a suggestion how to handle this.
Daniel
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
See my comments below.
If your control that is being designed was created with "Add Inherited User Control", you can rely on DesignMode from the class that you are deriving from.
The simple solution for me was to just look at the name of the current process, and see if it was 'devenv'.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
I usually just do:
public SevenHolder() { InitializeComponent(); if ( DesignMode ) return;
// now, put all my custom code here with no other checks for running instance or singleton... }
-- modified at 2:28 Tuesday 28th March, 2006
|
| Sign In·View Thread·PermaLink | 5.00/5 (2 votes) |
|
|
|
 |
|
|
Of course. That's my "second approach".
My personal coding style (that probably came from coding C/C++ for a long time), is that each and every possible point of failure should be checked. Perhaps somebody has a bug in the singleton? After all it could be someone else's code.
Nadav
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
nadav,
This should be your first approach. Else, you assume that your singleton is there, is initialized, is configured and is ready for the work you want to do with it while in design mode. If that singleton is not behaving, is NOT your designer that should pick that up. To get your form running in the designer, your only concern in your constructor should be only take care that the designer works, and not that some other dependent object is valid.
Tutu.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
It's a wellknown fact that if (DesignMode) does not always yield correct results. On several occasions, people have noticed that it returns false even though you are actually running inside the Designer. There are other workarounds (eg using LicenseManager), but apparently this way isn't foolproof either.
The best way I have been able to find so far is to see if the currently running process is "devenv", but this comes with a performance hit, so when doing it repeatedly, one would prefer to check this once and cache the result throughout the lifetime of the application.
This being said, if making it "not break" by checking null is an option, I'd definately go for that.
|
| Sign In·View Thread·PermaLink | 5.00/5 (2 votes) |
|
|
|
 |
|
|
Filip Duyck wrote: It's a wellknown fact that if (DesignMode) does not always yield correct results
This is not true. DesignMode is one of the most commonly misinterpreted states, and it bit me too.
DesignMode returns true when the class that is calling it, is being designed.
If the class that is being designed, is derived from another base class, DesignMode returns false when called from the base class, because the base class is not what is being 'designed'.
Hence, DesignMode doesn't really answer the simpler question of is 'my code running in the IDE'
The simple solution for me, was to just look at the name of the current process, and compare it to 'devenv', which told me that my code was running in the IDE, not in my application.
|
| Sign In·View Thread·PermaLink | 4.00/5 (1 vote) |
|
|
|
 |
|
|
tonyt wrote: If the class that is being designed, is derived from another base class, DesignMode returns false when called from the base class, because the base class is not what is being 'designed'.
Weird, because normally calls from the base class of an derived class run in the context of the derived class. For example, querying this.GetType() from the base class would return the type of the derived class. I'm not arguing against what you say, because frankly I have no idea, just saying.. 
tonyt wrote: The simple solution for me, was to just look at the name of the current process, and compare it to 'devenv', which told me that my code was running in the IDE, not in my application.
Indeed, that's what I do too. Additionally, I wrap that call in a static class and cache the result, so I only once get the performance hit that comes with it.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
This is not true either 8)
I've found this in msdnmag: "Note that the DesignMode property shouldn't be checked from within the constructor, or any code called by the constructor. Since the constructor is called prior to the component being assigned to a site, and it's the site that determines whether a component is in design mode or not, an object can't know whether it's in design mode until after the constructor has finished executing."
This is why you can't find out whether you're in design mode or not from ctor of Control. DesignMode property work correctly only from methods. I don't actually know the correct way to check this property, but I usually use this approach:
protected override void OnCreateControl() { if (this.DesignMode) { // Do something... } base.OnCreateControl(); }
This works fine for me..
p.s. Thanks for your article, it really helped me a lot
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Let me clarify:
public class SomeBaseForm : Form { public SomeBaseForm() { if( ! this.DesignMode ) { // here is where you get burned, because // DesignMode is always false when this // is a base class of the class that is // being designed. } } }
public class DesignedForm : SomeBaseForm { public DesignedForm() { // Here, DesignMode will be true // when this class is being designed } }
My soution:
static bool IsRunning = string.Compare(Process.GetCurrentProcess().ProcessName, "devenv", true) != 0;
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
General News Question Answer Joke Rant Admin
|