Nick Ma
Nick Ma

Reputation: 31

The name 'InitializeComponent' does not exist in the current context + Visual Studio 2019 + Windows forms

Newbie here struggling the way into the Matrix. I'am coding my first program ( VS 2019), and using a WinForms Application I get the following error

Screenshot

Since this code is autogenerated I have no clue of what can be wrong

Upvotes: 1

Views: 1062

Answers (1)

user2864740
user2864740

Reputation: 61975

tldr; Update the files (Form1.cs and Form1.Designer.cs) as needed to ensure that they define the relevant parts of the the same partial class - namespace and class name.

The InitializeComponent method (and components) should be defined in a partial code-behind for the design surface. In this case that is Form1.Designer.cs - inspect that file to resolve the error.

The compilation error sounds like the classes are in different namespaces or different type names (and thus are not partial to eachother), possibly from invalid generation or an unsynchronized change.

  • Form1.cs's Form1 class cannot find the InitializeComponent method which should be defined via the partial in Form1.Designer.cs; and
  • Form1.Design.cs cannot override Dispose which comes from the Form base-type inherited from Form1 in Form1.cs.

From the shown code, both files should have a Form1 class defined namespace strnull.

Note: the strnull namespace seems odd, perhaps an invalid template / generator. Update both files as needed to ensure that they define the relevant parts of the the same partial class.

Upvotes: 2

Related Questions