开发者

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 de开发者_高级运维signer, 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.


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.


One place where I've had problems like this with forms in the past is if I've removed the parameterless constructor. The designer doesn't know how to create the form without a no-arg constructor.

If you only want your form only to be created with a constructor with parameters, the solution is to keep the parameterless constructor but make it private. The designer can still use it that way, but no one else can.


The best advice is also the most confronting advice: get the previous version back from source control.

If that is not possible you will have to recreate the form using the designer.

Never touch designer files unless you absolutely know what you are doing.

I am sorry that the new isn't any better than this.


It turns out that the real root cause is compiling in .NET 3.5 mode even though the designer is biased toward .NET 4.0. It works fine when compiling against .NET 4.0, but as soon as the .resx changes to have references to "2.0.0.0" drawing components instead of "4.0.0.0", the designer freaks out.


The only solution is to manually add the Controls.Add calls. This happens to me a lot in Visual Studio 2008 when I'm working with MenuStrips. The problem might still be present in 2010.

Or, as others have suggested, recreate the form.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜