24

After several days of happily hacking away on this C# app using Visual Studio 2008, I get struck by a barrage of error dialogs showing:

Code generation for property 'valueMember' failed.
Error was: 'Object reference not set to an instance of an object.'

This happens now often when I make a tiny change in the designer, e.g. shift a control a few pixels, and then try to save. Several such error dialogs appear each second, keeping me busy cancelling all those by hammering the Enter key while trying to get alt-F4 to get VS to close.

Eventually I do get VS to close and to save the changes I made. After restarting VS, I do "clean" on the entire project, then "build" and everything works fine, the app runs fine, no problems.

Until I make another slight change in the form designer.

I don't know about any property valueMember in my app.

This makes me crazy, it is a real showstopper for my project. Any help is appreciated.

1
  • 3
    Sounds like the designer has a problem with the *.designer.cs files that sit behind each of your forms. That would be where to start looking.
    – Polyfun
    Commented Nov 22, 2012 at 17:12

9 Answers 9

23

Try to Close and reopen the Visual Studio. maybe it seem silly, but it works!!

1
  • 2
    A minor time-saver, but closing and opening the solution within Visual Studio worked for me. I had to discard the changes to the particular Form causing the error dialog to show before I could close it though. Commented Nov 10, 2020 at 12:21
16

You can debug the designer using another visual studio and attach to process. If you got exception it should be easy to find it that way. In general when openning the designer the constructor and of course initializeComponent is running.

5
  • IMHO, this should be the accepted answer. took me right to the problematic code. great tip dude!
    – Nissim
    Commented Dec 11, 2014 at 21:49
  • Excellent. I never would have thought of this.
    – CathalMF
    Commented Jul 8, 2015 at 16:38
  • 1
    This would of been good if it was understandable and a bit more details (e.g. which process + what to look for)...
    – stigzler
    Commented Jan 27, 2019 at 21:52
  • I agree with @stigzler. More detail is needed about how to do this. Commented Mar 18, 2019 at 17:56
  • stackoverflow.com/questions/39648/…
    – shanif
    Commented Mar 19, 2019 at 20:03
13

As this is happening at design time, it is likely that you have a custom control which requires a parameter or other value which does not have a default.

When in design view in Visual Studio; a control instance is created to render it on the visual editor, but if the control requires a property to be set before it can be rendered, it will result in an error.

Can you check that all custom controls have default values, and anything referenced in the constructor that cannot have a default is wrapped by DesignMode property - see http://msdn.microsoft.com/en-us/library/system.componentmodel.component.designmode.aspx.

2
  • Thanks for the hints. I don't think I have custom controls in this project I inherited to maintain, but a global search does find 'valueMember' properties in some X.designer.cs files. When the errors appear again, I will study that code. This leaves the problem why the error message appears in infinite numbers instead of only once.
    – Roland
    Commented Nov 23, 2012 at 8:52
  • THis is a huge pile of inherited code, and I have added default values to lots of variables. This is a good design principle anyway. This resulted in much fewer null-ref exceptions in general, and the VS problem also doesn't return anymore.
    – Roland
    Commented Sep 5, 2014 at 12:37
2

Similiar to @Chanipoz's answer (close/re-open) my component-rich/user-controls-everywhere forms app started to compile happily after I closed down the main form designer window.

I've had this code stack for years and have never seen the error until today. Not sure where it's coming from. But, something today about having the form open in the designer made everything unhappy. Simply closing it off of the screen made it all go smooth.

2
  • Did you also suffer from a barrage of error dialogs that came in faster than you could close them?
    – Roland
    Commented Jan 28, 2015 at 10:43
  • only seemed like a handful.. but.. it was very dedicated to issuing those error boxes as long as that particular form was open in the designer
    – bkwdesign
    Commented Jan 28, 2015 at 19:28
2

I had to face this problem. As I have found the solution below I am facing this issue in my customized control.

we need to implement like this

[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public MyCustomclass _Prperty { get; set; }
1
  • My problem happened 10 years ago with a VS version 14 years old. Did you see the same problem recently with VS2022?
    – Roland
    Commented Jan 28, 2022 at 15:49
1

Use another instance of Visual Studio to attach to the first instance of visual studio.

Go to Debug-> Attach To Process and look for the devenv.exe process. Since you'll have two devenv.exe processes running you'll probably want to pick the one with the lower ID, that's usually the first instance of visual studio that was run.

1
  • Thanks for your answer. As I have upgraded from VS2008 to VS2017 in several steps over the past 7 years and am working on much different code, much more structured, I have not seen this problem anymore. But in case someone might experience it today, your answer may be helpful.
    – Roland
    Commented Sep 24, 2019 at 13:25
0

I had to face this problem. As I have not found the solution (much inheritance), I can tell: .SuspendLayout() and .ResumeLayout() may be missing in code or one of them. The same is with .BeginInit() and .EndInit(). It is expected between them, that there will be = new ... and some settings for properties. Maybe someone facing this problem would find the solution with this information.

1
  • 1
    Also, while there is inheritance, important are modifiers: public, private, protected - for controls.
    – pbies
    Commented Mar 18, 2014 at 23:40
0

The problem is missing initialization code for a public property on the control. This will be added for you when you add the control to the designer, but if you replace a control with a derived control, or update the component, then the designer does not know how to deal with this.

If you have a control (wincontrol) with a public property PropertyA, and you add it to a form (myForm), then the designer will add all the necessary initialization for properties into myForm.Designer.cs. Something like;

Wincontrol1.PropertyA = new List<widget>();

It is not uncommon to need to modify a control slightly, lets say we have a new control MyWinControl

public partial class MyWinControl : WinControl
{
    public List<wodget> PropertyDer1;
    protected List<wodget> PropertyDer2;
}

If you sub this new control for the old control in myForm.Designer.cs, then you may well encounter this issue. The reason is that PropertyDer1 has no initialization in the winforms designer. PropertyDer2 won't cause any issues because it is protected. Similarly if you had a custom component and you add a new public property after the component has been added to a form.

If however, you deleted the instance of WinControl on the form, and dragged an instance of the MyWinControl onto the form instead, the proper initialization would occur and you would not see the error. The designer will have created the new control like this

Wincontrol1.PropertyA = new List<widget>();
Wincontrol1.PropertyDer1= new List<wodget>();

There are two easy solutions that do not require hiding the property from the designer. 1. If the property doesn't need to be public, give it the right modifier 2. If the property does need to be public, then just edit the code in the myForm.Designer.cs as in the code above to add the missing initializer

0

If could be of help I just detected a case that brings that same error message, impossible to take away : I am developing an application in French, and I had to create a ToolStripMenuItem with an accented word in it like "annulées". The system generated a menu item like "annuléesToolStripMenuItem" and the accent is the culprit. Enough to delete the item, create it again in English and the just change the Text property of the menu item. Hope it will be of some help.

2
  • Did the unicode method name just cause a build error or run exception, or did this generate many many many error dialogs, faster than you can close them?
    – Roland
    Commented Jul 13, 2021 at 8:15
  • No, it just generate a quite long error message and stopped the publishing. Correcting the accented letter did solve the problem.
    – Claudio-it
    Commented Jul 15, 2021 at 14:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.