Incorrect association/nesting of files in Visual Studio project

As I have mentioned in a previous article, improper association/nesting of form’s Class file (.vb), Designer file (.Designer.vb) and Resource file (.resx), could occur when adding existing files from another project, as shown below:

Problem
Incorrect Nesting of .Designer.vb and .vb files in Solution Explorer in Visual Studio

Incorrect Nesting in Solution Explorer

You may not be able to reproduce the problem when adding multiple files, it happens automatically. However, as VS is a computer program, there must be some reason (may be a bug in VS) for this problem. I don’t know the reason, but I know the solution at-least.

The list of files in a project is stored in its corresponding “.vbproj” file. “.vbproj” file is internally actually a pure XML file. Following is the content of the “.vbproj” file which was automatically created when a VB.NET Windows Application project was created:

The “.vbproj” file can be opened/edited in any text editor like Notepad, Notepad++, WordPad, Ms Office Word etc.

This file contains much information about the project but our primary focuses are the 3rd and 4th ItemGroup tags. The first ItemGroup is of References, second is of Imports, third is of project code files (highlighted with light-blue background) and fourth is of Resources (highlighted with light-yellow background). Now the above file is perfect. (Form’s “.resx” file is not created initially; it is created only after a resource (icon, image etc.) is added to a form.) Now look at the following table which should clear your doubt about improper nesting:

XML code for .vb files
Proper code Improper code
XML code for .resx files
Proper code Improper code
Solution

As you can see in the above table, in the improper code for “.vb” file, the “Form1.Designer.vb” is contained within a single tag (for ex. <Compile Include=”Form1.Designer.vb” />) instead of a pair tag (for ex. <Compile Include=”Form1.Designer.vb”> … </Compile>), and <DependUpon> tag is missing. So, what you need to do is:

  • Close the project/solution if it is open.
  • Open the project’s “.vbproj” in any text editor.
  • Remove the space and slash from the end of <Compile Include=”Form1.Designer.vb” /> to make it <Compile Include=”Form1.Designer.vb”>
  • Replace the <Compile Include=” with <DependUpon>     and     “> with </DependUpon>
  • Make similar replacement for “.resx” files if applicable.
  • Save the file and open it again in Visual Studio, all files should be nested correctly now.

Even if the files are nested improperly, if project is run, the compiler will not display any error because there is no error in project, no reference is missing; it is just the project’s file which is just displaying incorrect nesting.

If a form’s “.Designer.vb” file is opened, only designer code is showed for editing instead of form’s design/window; and if “.vb” file is opened, the form’s design/window is showed! As VB code is not shown directly, you can access it by going to design view, right clicking on the form and selecting “View Code” option from popup menu OR by selecting “.vb” file in Solution Explorer and clicking the “View Code” button OR right-clicking on the “.vb” file in Solution Explorer and clicking the “View Code” option from popup menu.

Problem

But if any more control is added in the design/window in an improperly nested file, and project is run, a compile time error will be introduced saying ‘Private Sub InitializeComponent()’ has multiple definitions with identical signatures; because 2 “InitializeComponent()” procedures, one in “.Designer.vb” file and another in “.vb” file exist. There was no error until a new control is added because only 1 “InitializeComponent()” procedure was existing in “.Designer.vb” file; but as soon as a new control is added, 2nd “InitializeComponent()” procedure is created in “.vb” file and this error occurs.

Solution

So, if you want to add controls, you should correct the nesting OR you can merge 2 “InitializeComponent()” procedures into 1 procedure in “.vb” file and moving the “Friend WithEvents…” statements from “.Designer.vb” file to “.vb” file; there must not “InitializeComponent()” procedure in the “.Designer.vb” file. Whenever new control is added, its code is appended in the “.vb” file in improper nesting situation.

 

Rate this article

Incorrect association/nesting of files in Visual Studio project
7 votes, 4.43 avg. rating (88% score)

2 Comments.

  1. Thank you so much! I get to keep some of my hair after all.

  2. Thank-you for sharing this! I’ve spent hours trying to get this resolved.