19

I have a project written by someone else with .NET framework 4 I have a problem with one of the forms (others opening correctly).

When I try to open Form1 in "Design mode" Visual Studio 2017 shows the error screen and returns "Failed to parse method 'InitializeComponent'. The parser reported the following error 'Invalid symbol kind: NamedType'. Please look in the Task List for potential errors."

with Call Stack

at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomParser.OnMethodPopulateStatements(Object sender, EventArgs e)
at System.CodeDom.CodeMemberMethod.get_Statements()
at System.ComponentModel.Design.Serialization.TypeCodeDomSerializer.Deserialize(IDesignerSerializationManager manager, CodeTypeDeclaration declaration)
at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)
at System.ComponentModel.Design.Serialization.BasicDesignerLoader.BeginLoad(IDesignerLoaderHost host) 
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Adam
  • 191
  • 1
  • 1
  • 5
  • post relevant code – Rahul Aug 06 '18 at 14:33
  • show class code or make it more clear. – Arslan Ali Aug 06 '18 at 14:36
  • Try opening the the Form1.designer.cs file and see if there is a compilation error somewhere. There must be some change or line that Visual Studio designer doesn't likes. – Alejandro Aug 06 '18 at 14:39
  • 1
    You need to open the designer file and fix it by hand. We can't recommend the fix without seeing the problem code. –  Aug 06 '18 at 14:39
  • Sorry ... I'm new here ... how can I add code to this post? (Form1.cs has above 12000 rows and Form1.resx has above 7200 rows) – Adam Aug 06 '18 at 15:04
  • I fixed it by creating new Form and moving all properties one by one from the old form to the new one. – Adam Aug 13 '18 at 09:15

5 Answers5

38

To add to the body of knowledge, I was getting this same error. My situation was I was upgrading a solution created by JetBrains dotPeek. Its solution versioned as 10.0 (VS2008) and converted to 15.0 (VS2015) for VS2017, and is a .NET 3.5 target class library. After opening the form up (in the old style with no designer file) it always threw the error when opening the designer (but running the class library was fine). On carefully reviewing every line of InitializeComponent, I found a "this.Name = nameof(formname)". Looking at another form that worked, I saw "this.Name = "formname"". Once I changed the line over to the other approach, the designer opened perfectly!

Will DP
  • 381
  • 3
  • 3
  • 1
    Works for me, but aside of this, I see other problems too, with the resx file attached to the form, still not resolved. – IFink Oct 15 '18 at 16:27
  • Works for me ! tx to save my day to reverse engineering an old project with source lost and used JetBrains dotPeek to recreate it ! Because it was a very simple project it seems i don't have any issue with resx file (cf. Ifink) – bau Jan 24 '20 at 15:47
  • You may have to unload your `form.cs` file from project and then Add -> Existing Item -> yourFormName.cs so it will look properly – Potato Aug 21 '20 at 09:13
  • Thank you!! You saved me a bunch of grief. – Mark Ainsworth Jul 28 '22 at 15:01
9

Go to InitializeComponent() function and search for nameof(). If found just replace value part to accurate value.

In my case it was

this.Text = nameof (Form1);

Changed to:

this.Text = "Page1";
Ashish Gehlot
  • 483
  • 8
  • 16
  • This is what worked for me as well (this.Text and not this.Name). I had generated the solution through DotPeek as mentioned in above answer. – Varun Sharma Jan 17 '23 at 20:03
1

Another possible cause: the designer parser doesn't seem to recognize the C# curly braces initializer syntax:

Changed this:

this.myLabel = new Label { Name = "myLabel", Text = "My Label", Anchor = AnchorStyles.Left, AutoSize = true };

To this:

this.myLabel = new System.Windows.Forms.Label();
this.myLabel .Anchor = System.Windows.Forms.AnchorStyles.Left;
this.myLabel .AutoSize = true;
this.myLabel .Location = new System.Drawing.Point(3, 30);
this.myLabel.Name = "myLabel";
this.myLabel.Size = new System.Drawing.Size(38, 15);
this.myLabel.TabIndex = 1;
this.myLabel.Text = "My Label";
emanresu
  • 141
  • 2
  • 5
0
  1. This may also due to missing type(s) referred to in a not referenced assembly
  2. Corrupt assembly (false signing or public key token mismatch of assemblies referred to). This is mostly due to version mismatches (strongly named assemblies) or patches of assemblies.

Please check the assemblies, clean, rebuild and see, if this error still shows up. Maybe restart Visual Studio.

Cheers

Martin.Martinsson
  • 1,894
  • 21
  • 25
0

Make sure the correct System namespace is being referenced.

In my case I had a System folder in my project (so project.system namespace) that was confusing namespaces and causing these errors because System references inside InitializeComponent() were failing.

MelOS
  • 595
  • 5
  • 10