Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#
Article

How to avoid WSODs in the Visual Studio 2005 Designer

Rate me:
Please Sign up or sign in to vote.
4.75/5 (22 votes)
3 Apr 20063 min read 123.7K   209   43   37
An article on how to deal with designer exceptions in Visual Studio 2005.

Sample Image - 01 article.jpg

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:

Image 2

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:

  1. Open a new Visual Studio instance.
  2. Open any source file. This is required so that Visual Studio lets you attach to a process.
  3. 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.
  4. Close the problematic form, and reopen it.
  5. This should result in an exception caught inside your code in the second instance of Visual Studio!

    Image 3

    In our case, the following code was causing the problem:

    C#
    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:

    C#
    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:

    C#
    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:

    C#
    public SevenHolder()
    {
        InitializeComponent();
    
        if (this.DesignMode)
            return;
    
        Singleton sing = Singleton.Instance();
    
        if (sing != null)
        {
            this.labelSevenHolder.Text = sing.Information.ToString();
        }
    }
  6. 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.

    Image 4

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralI don't even get a WSOD anymore... Pin
primem0ver14-Mar-11 18:04
primem0ver14-Mar-11 18:04 
GeneralSuggestion: Pin
sherifffruitfly24-May-09 8:57
sherifffruitfly24-May-09 8:57 
GeneralMore info on this Pin
tonyt10-Dec-08 6:27
tonyt10-Dec-08 6:27 
GeneralWorks great for WPF too! Pin
Kenrae4-Nov-08 21:59
Kenrae4-Nov-08 21:59 
Thanks! I had this problem in a WPF application. Your method can be used with WPF apps too.
GeneralRe: Works great for WPF too! Pin
nadav744-Nov-08 22:02
nadav744-Nov-08 22:02 
GeneralDesigner View problem Pin
ramki_mars15-May-08 18:28
ramki_mars15-May-08 18:28 
GeneralProblem in viewing designer of windows form c# Pin
ramki_mars11-Apr-08 1:17
ramki_mars11-Apr-08 1:17 
GeneralAnother simple way to debug this type of error Pin
Natza Mitzi23-Dec-07 1:45
Natza Mitzi23-Dec-07 1:45 
GeneralRe: Another simple way to debug this type of error Pin
SerialHobbyist8-Mar-09 1:27
SerialHobbyist8-Mar-09 1:27 
Generalthis.DesignMode Pin
Jakub Mller14-Aug-07 21:19
Jakub Mller14-Aug-07 21:19 
GeneralRe: this.DesignMode Pin
deerchao2-Nov-07 17:18
deerchao2-Nov-07 17:18 
GeneralGood article Pin
Christian Graus31-Jan-07 9:24
protectorChristian Graus31-Jan-07 9:24 
GeneralRe: Good article Pin
nadav7431-Jan-07 9:32
nadav7431-Jan-07 9:32 
QuestionRe: Good article Pin
leoprex5-Feb-07 6:36
leoprex5-Feb-07 6:36 
AnswerRe: Good article Pin
Christian Graus5-Feb-07 8:57
protectorChristian Graus5-Feb-07 8:57 
GeneralRe: Good article Pin
leoprex5-Feb-07 10:05
leoprex5-Feb-07 10:05 
Generalactivex as a singleton?! Pin
lnae15-Nov-06 1:43
lnae15-Nov-06 1:43 
GeneralNo menu entry 'Exceptions' Pin
Wilhelm Berg2-Aug-06 20:57
Wilhelm Berg2-Aug-06 20:57 
JokeWSOD Pin
robbyt4-Apr-06 1:24
robbyt4-Apr-06 1:24 
JokeRe: WSOD Pin
nadav745-Apr-06 0:24
nadav745-Apr-06 0:24 
GeneralExceptions in UserControls Pin
DanielFaensen31-Mar-06 3:29
DanielFaensen31-Mar-06 3:29 
GeneralRe: Exceptions in UserControls Pin
tonyt4-Apr-06 13:29
tonyt4-Apr-06 13:29 
GeneralRe: Exceptions in UserControls Pin
DanielFaensen5-Apr-06 4:23
DanielFaensen5-Apr-06 4:23 
Questionif ( DesignMode ) ? Pin
Corneliu Tusnea27-Mar-06 20:27
Corneliu Tusnea27-Mar-06 20:27 
AnswerRe: if ( DesignMode ) ? Pin
nadav7427-Mar-06 20:35
nadav7427-Mar-06 20:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.