4

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.

Community
  • 1
  • 1
Reinderien
  • 11,755
  • 5
  • 49
  • 77
  • 1
    Sometimes the designer is ornery. I haven't figured out a good reason for this yet, but I don't waste much time looking. It's so much easier to recreate the form and be on with my life. If there are really that many controls that you don't want to recreate it, try copying and pasting the controls from the old form to the new one. – Cody Gray - on strike Apr 02 '11 at 13:46
  • For me the following worked (is cumbersome but it might be of help for some people): *Rebuild the project - Unload the project - Reload the project*. As a downside it will close all of the open files from that project but it is the only way I could make it work on my end. – Felipe Correa Oct 15 '19 at 16:46

5 Answers5

4

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 2
    I wish that it actually threw an exception and told me what's happening, but all of the failure is silent. – Reinderien Apr 02 '11 at 16:38
  • 1
    I've never heard of this being silent. But quickly clicked away without thinking too much about it. Which works, no more exception. But no more form either. – Hans Passant Apr 02 '11 at 16:56
  • What might be worth adding here: if the Control's code implements a custom IDisposable..Dispose() method / mechanism, this one is also called by the designer so you got to make sure the code in .Dispose() is also Designer / non-runtime safe etc – Jörg Battermann Sep 10 '17 at 11:11
3

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.

Ryan Lundy
  • 204,559
  • 37
  • 180
  • 211
  • I do indeed have this situation where there was no parameter-less constructor, and I got really excited when I saw this. Unfortunately, after adding the additional constructor the problem persisted. – Reinderien Apr 02 '11 at 16:43
  • 1
    Also, make sure the parameterless constructor contains the call to `InitializeComponent`. – Ryan Lundy Apr 02 '11 at 23:49
2

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.

Emond
  • 50,210
  • 11
  • 84
  • 115
1

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.

Reinderien
  • 11,755
  • 5
  • 49
  • 77
  • 1
    I've never seen this. I don't think the designer is "biased toward .NET 4.0". I do all my work in VS 2010, and the vast majority of that is compiling WinForms applications that target .NET 3.5. However, the obvious solution is to create an app targeting 3.5 and *keep* it that way. It sounds to me like the problem only crops up when you switch back and forth; a pretty unusual case. – Cody Gray - on strike Apr 03 '11 at 05:32
  • It's related to how I've set up the project, to have targets for .NET 3.5/4.0, debug/release, x64/x86/anycpu with as little redundancy as possible. Refer to http://code.google.com/p/benfwaves/source/browse/trunk/BenfWaves.Client/BenfWaves.Client.csproj . – Reinderien Apr 03 '11 at 21:47
  • Interesting... I just set up multiple build configurations for .NET 2 and .NET 4 on a class library the other day. It doesn't use the form designer, so I haven't experienced any such problems, but now I'm curious about your approach. It was my understanding that you couldn't do that kind of multiple targeting in VS 2010. There is a single target framework for *all* build configurations; it's not customizable for different build configurations. The only solution would be to manually switch back and forth, resaving the project each time. Is there a better way? – Cody Gray - on strike Apr 04 '11 at 09:08
  • Looking at the linked code, it appears the solution involves adding a `` to the project file by hand. The limitation is therefore just that the Visual Studio doesn't support it? – Cody Gray - on strike Apr 04 '11 at 09:16
  • Yes. I had to do extensive editing to that project file myself. Thankfully, it (more or less) works with Visual Studio after having written it. – Reinderien Apr 04 '11 at 13:00
  • Yup, it works for me too. Well, mostly. The IDE seems to ignore conditions on references, so it balks that `System.Core` doesn't exist when targeting .NET 2.0. But much better than what I had; +1 for that alone. :-) – Cody Gray - on strike Apr 04 '11 at 13:06
  • Thanks. You might also be interested in how I took that build setup and auto-populated the assembly properties. Refer to http://code.google.com/p/benfwaves/source/browse/trunk/BenfWaves.Library/Utils.cs and http://code.google.com/p/benfwaves/source/browse/trunk/BenfWaves.Library/Properties/AssemblyInfo.cs . – Reinderien Apr 04 '11 at 19:53
0

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.

Chris Laplante
  • 29,338
  • 17
  • 103
  • 134