Click here to Skip to main content
Licence 
First Posted 27 Mar 2006
Views 71,045
Bookmarked 43 times

How to avoid WSODs in the Visual Studio 2005 Designer

By | 3 Apr 2006 | Article
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:

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!

    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();
        }
    }
  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.

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

About the Author

nadav74

Software Developer (Senior)

Israel Israel

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralI don't even get a WSOD anymore... Pinmemberprimem0ver18:04 14 Mar '11  
GeneralSuggestion: Pinmembersherifffruitfly8:57 24 May '09  
GeneralMore info on this Pinmembertonyt6:27 10 Dec '08  
GeneralWorks great for WPF too! PinmemberKenrae21:59 4 Nov '08  
GeneralRe: Works great for WPF too! Pinmembernadav7422:02 4 Nov '08  
GeneralDesigner View problem Pinmemberramki_mars18:28 15 May '08  
GeneralProblem in viewing designer of windows form c# Pinmemberramki_mars1:17 11 Apr '08  
GeneralAnother simple way to debug this type of error PinmemberNatza Mitzi1:45 23 Dec '07  
GeneralRe: Another simple way to debug this type of error PinmemberSerialHobbyist1:27 8 Mar '09  
Generalthis.DesignMode PinmemberJakub Mller21:19 14 Aug '07  
GeneralRe: this.DesignMode Pinmemberdeerchao17:18 2 Nov '07  
GeneralGood article PinstaffChristian Graus9:24 31 Jan '07  
GeneralRe: Good article Pinmembernadav749:32 31 Jan '07  
QuestionRe: Good article Pinmemberleoprex6:36 5 Feb '07  
AnswerRe: Good article PinstaffChristian Graus8:57 5 Feb '07  
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

GeneralRe: Good article Pinmemberleoprex10:05 5 Feb '07  
Generalactivex as a singleton?! Pinmemberlnae1:43 15 Nov '06  
GeneralNo menu entry 'Exceptions' PinmemberLoneRanger20:57 2 Aug '06  
JokeWSOD Pinmemberrobbyt1:24 4 Apr '06  
JokeRe: WSOD Pinmembernadav740:24 5 Apr '06  
GeneralExceptions in UserControls PinmemberDanielFaensen3:29 31 Mar '06  
GeneralRe: Exceptions in UserControls Pinmembertonyt13:29 4 Apr '06  
GeneralRe: Exceptions in UserControls PinmemberDanielFaensen4:23 5 Apr '06  
Questionif ( DesignMode ) ? PinmemberTutu20:27 27 Mar '06  
AnswerRe: if ( DesignMode ) ? Pinmembernadav7420:35 27 Mar '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 3 Apr 2006
Article Copyright 2006 by nadav74
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid