Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Forms designer is permanently broken for a certain form

This is a follow-up to All controls on a form are invisible, now that I know a little more about it.

I have a certain form that was created with the Windows Forms designer of Visual Studio 2010. It was working fine until some time this week. Now when I make any change to the form and the designer recreates the .designer.cs file, all binding members are set to "none", and all Controls.Add calls are removed. The controls still are visible in the designer, but then when I run the project the controls are all invisible (due to there being no Controls.Add calls). If I close and reopen the designer, the form is blank.

There are no errors, warnings or messages indicating why the designer is being a jerk, and I really don't want to have to recreate every single control on that form, but it's looking like I might have no choice.

like image 743
Reinderien Avatar asked Dec 27 '22 22:12

Reinderien


1 Answers

The designer is vulnerable to exceptions that are raised by code that runs at design-time. Which include the constructor of the controls and methods like OnPaint(), OnResize() etcetera. If you have code in them that won't work properly at design time, like depending on a file being in the default working directory, a dbase server being connectable, etcetera then that code can bomb with an exception.

You'll first notice the crash screen that the designer puts up, the stack trace it shows is not often useful to diagnose the cause since it has lots of methods that are internal to the designer or the code serializer. A secondary effect is that you can lose the content in the InitializeComponent() method when the code serializer crashes when trying to retrieve a property value to generate code for it. Which no doubt happened in your case when you see Controls.Add() calls missing.

Getting the designer.cs file saved after such a mishap then gets you into trouble. Repairing the damage can be tricky, restoring the form source code files from your VCS is usually your best bet.

like image 105
Hans Passant Avatar answered Apr 08 '23 19:04

Hans Passant