WinForms and the big red 'X' of doom

Originally posted to Shawn Hargreaves Blog on MSDN, Monday, November 22, 2010

You are making a WinForms app. All is going well, until one day your custom control disappears. In its place you just see a big red cross:

Untitled

What gives?

Let us back up to remind ourselves how exceptions work:

WinForms extends this system in one important way:

I don't know why WinForms decided to do this, but it's kinda annoying because if anything goes wrong in your OnPaint code, the debugger never gets to see the exception so has no chance to show you the exception message!

To debug such problems, open up the Debug \ Exceptions menu in Visual Studio and check the Thrown box next to Common Language Runtime Exceptions. Now the debugger will break whenever any exception is thrown, regardless of whether or not it is caught further up the stack, so you can see the original message and callstack in spite of WinForms trying to catch this error.

Blog index   -   Back to my homepage