Click here to Skip to main content
11,637,176 members (67,663 online)
Click here to Skip to main content

How to avoid WSODs in the Visual Studio 2005 Designer

, 3 Apr 2006 89.2K 198 43
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

nadav74
Software Developer (Senior)
Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralI don't even get a WSOD anymore... Pin
primem0ver14-Mar-11 18:04
memberprimem0ver14-Mar-11 18:04 
GeneralSuggestion: Pin
sherifffruitfly24-May-09 8:57
membersherifffruitfly24-May-09 8:57 
GeneralMore info on this Pin
tonyt10-Dec-08 6:27
membertonyt10-Dec-08 6:27 
GeneralWorks great for WPF too! Pin
Kenrae4-Nov-08 21:59
memberKenrae4-Nov-08 21:59 
GeneralRe: Works great for WPF too! Pin
nadav744-Nov-08 22:02
membernadav744-Nov-08 22:02 
GeneralDesigner View problem Pin
ramki_mars15-May-08 18:28
memberramki_mars15-May-08 18:28 
GeneralProblem in viewing designer of windows form c# Pin
ramki_mars11-Apr-08 1:17
memberramki_mars11-Apr-08 1:17 
GeneralAnother simple way to debug this type of error Pin
Natza Mitzi23-Dec-07 1:45
memberNatza Mitzi23-Dec-07 1:45 
GeneralRe: Another simple way to debug this type of error Pin
SerialHobbyist8-Mar-09 1:27
memberSerialHobbyist8-Mar-09 1:27 
Generalthis.DesignMode Pin
Jakub Mller14-Aug-07 21:19
memberJakub Mller14-Aug-07 21:19 
GeneralRe: this.DesignMode Pin
deerchao2-Nov-07 17:18
memberdeerchao2-Nov-07 17:18 
GeneralGood article Pin
Christian Graus31-Jan-07 9:24
staffChristian Graus31-Jan-07 9:24 
GeneralRe: Good article Pin
nadav7431-Jan-07 9:32
membernadav7431-Jan-07 9:32 
QuestionRe: Good article Pin
leoprex5-Feb-07 6:36
memberleoprex5-Feb-07 6:36 
AnswerRe: Good article Pin
Christian Graus5-Feb-07 8:57
staffChristian Graus5-Feb-07 8:57 
GeneralRe: Good article Pin
leoprex5-Feb-07 10:05
memberleoprex5-Feb-07 10:05 
Generalactivex as a singleton?! Pin
lnae15-Nov-06 1:43
memberlnae15-Nov-06 1:43 
GeneralNo menu entry 'Exceptions' Pin
LoneRanger2-Aug-06 20:57
memberLoneRanger2-Aug-06 20:57 
JokeWSOD Pin
robbyt4-Apr-06 1:24
memberrobbyt4-Apr-06 1:24 
JokeRe: WSOD Pin
nadav745-Apr-06 0:24
membernadav745-Apr-06 0:24 
GeneralExceptions in UserControls Pin
DanielFaensen31-Mar-06 3:29
memberDanielFaensen31-Mar-06 3:29 
GeneralRe: Exceptions in UserControls Pin
tonyt4-Apr-06 13:29
membertonyt4-Apr-06 13:29 
GeneralRe: Exceptions in UserControls Pin
DanielFaensen5-Apr-06 4:23
memberDanielFaensen5-Apr-06 4:23 
Questionif ( DesignMode ) ? Pin
Tutu27-Mar-06 20:27
memberTutu27-Mar-06 20:27 
AnswerRe: if ( DesignMode ) ? Pin
nadav7427-Mar-06 20:35
membernadav7427-Mar-06 20:35 
AnswerRe: if ( DesignMode ) ? Pin
Tutu28-Mar-06 22:51
memberTutu28-Mar-06 22:51 
GeneralRe: if ( DesignMode ) ? Pin
Filip Duyck29-Mar-06 0:10
memberFilip Duyck29-Mar-06 0:10 
GeneralRe: if ( DesignMode ) ? Pin
tonyt4-Apr-06 13:04
membertonyt4-Apr-06 13:04 
GeneralRe: if ( DesignMode ) ? Pin
Filip Duyck4-Apr-06 13:10
memberFilip Duyck4-Apr-06 13:10 
GeneralRe: if ( DesignMode ) ? Pin
susLamo14-Mar-07 1:52
membersusLamo14-Mar-07 1:52 
GeneralRe: if ( DesignMode ) ? Pin
tonyt4-Apr-06 13:21
membertonyt4-Apr-06 13:21 
GeneralRe: if ( DesignMode ) ? Pin
Filip Duyck4-Apr-06 13:24
memberFilip Duyck4-Apr-06 13:24 
GeneralRe: if ( DesignMode ) ? Pin
nadav7431-Mar-06 7:20
membernadav7431-Mar-06 7:20 
GeneralRe: if ( DesignMode ) ? Pin
imyrke3-Apr-06 4:11
memberimyrke3-Apr-06 4:11 
GeneralRe: if ( DesignMode ) ? Pin
nadav745-Apr-06 0:23
membernadav745-Apr-06 0:23 
GeneralGood idea! Pin
Peter Vrenken27-Mar-06 20:22
memberPeter Vrenken27-Mar-06 20:22 
GeneralRe: Good idea! Pin
nadav7428-Mar-06 2:40
membernadav7428-Mar-06 2:40 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150728.1 | Last Updated 3 Apr 2006
Article Copyright 2006 by nadav74
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid