views:

375

answers:

5

I've created a custom control using C#, .Net3.5, and Visual Studio 2008.

I'm then adding that control to another control by dragging it from the toolbox.

After doing this when I try to compile I get an error as follows:

Error   1 Warning as Error: Field 'MyNamespace.MyControl._myCustomControl' is never assigned to, and will always have its default value null

Looking at the Designer.cs file this is due to the fact that VS never generates the following code:

this._rgReportGallery = new MyNameSpace.MyCustomControl();

The field itself is there, and the code to add it to the control I'm putting it in, just the instantiation code is missing.

I can add that code manually and everything seems to work, but as soon as the designer.cs file is regenerated it goes missing again.

I've even successfully added a different custom control and had that code get generated.

Does anybody know what could be causing this?

+1  A: 

Have you adjusted the constructor of the custom control in any way? The default constructor for controls is a parameterless one, and I know I've had trouble in the past when creating a constructor for my controls that requires parameters. In those cases I always provide two constructors. That way the parameterless one remains for the design mode in VS.

Chris Holmes
+1  A: 

Also make sure your UserControl's constructors are public. I've had issues with internal UserControls and the VS2008 SP1 WinForms designer code generation. It failed to generate the allocation line like you mentioned.

Brent Miller
A: 

Even though the .Designer.cs files look like simple code, playing around in that code is very risky because it can easily break the Windows Forms Designer. I've heard a lot of people just abandoning the WinForms designer because they say it does not work. It's indeed easy to break, but if you don't try to play in the .Designer.cs file, it works like a charm.

If you have edited the Designer.cs file manually, it might explain why it keeps rewriting itself wrong. It's hard to know how to fix it though. If your UI is not too hard to redo, I would just redo it (whatever is in your main .cs file will be untouched). Otherwise you can look at a similar form to see a model of how exactly it should be formatted (the order of the statements is important).

Anthony Brien
A: 

Do you have the latest service pack for Visual Studio 2008? This usually happens when you don't have the latest service pack.

Ender
+1  A: 

I was having this issue and a reply here helped me find the issue, I was using the "internal" keyword for both my component accessibility and the constructor accessibility. As Brent mentions in a couple posts above mine, VS designer apparently has issues generating an instantiation line with anything except a public constructor, so never make a custom user control where the class accessibility and constructor(s) are anything but "public."

By the way, this occurred for me on Visual Studio 2005, and it doesn't surprise me that it also would happen on Visual Studio 2008. It likely happens on earlier versions of VS as well.

Peter Lacerenza
Jesus Christ, this designer is beyond stupid. Thanks for the pointer! It happens on VS 2010 too. Microsoft has apparently never used this as dog food on assemblies using internal keywords. Fascinating.
Jonas