Category Archives: Uncategorized - Page 4

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:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="https://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProductVersion>8.0.50727</ProductVersion>
    <SchemaVersion>2.0</SchemaVersion>
    <ProjectGuid>{C70F5285-0F5C-493C-96FB-FC311C404D55}</ProjectGuid>
    <OutputType>WinExe</OutputType>
    <StartupObject>Incorrect_Nesting.My.MyApplication</StartupObject>
    <RootNamespace>Incorrect_Nesting</RootNamespace>
    <AssemblyName>Incorrect Nesting</AssemblyName>
    <MyType>WindowsForms</MyType>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <DefineDebug>true</DefineDebug>
    <DefineTrace>true</DefineTrace>
    <OutputPath>bin\Debug\</OutputPath>
    <DocumentationFile>Incorrect Nesting.xml</DocumentationFile>
    <NoWarn>42016,41999,42017,42018,42019,42032,42036,42020,42021,42022</NoWarn>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <DefineDebug>false</DefineDebug>
    <DefineTrace>true</DefineTrace>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DocumentationFile>Incorrect Nesting.xml</DocumentationFile>
    <NoWarn>42016,41999,42017,42018,42019,42032,42036,42020,42021,42022</NoWarn>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Data" />
    <Reference Include="System.Deployment" />
    <Reference Include="System.Drawing" />
    <Reference Include="System.Windows.Forms" />
    <Reference Include="System.Xml" />
  </ItemGroup>
  <ItemGroup>
    <Import Include="Microsoft.VisualBasic" />
    <Import Include="System" />
    <Import Include="System.Collections" />
    <Import Include="System.Collections.Generic" />
    <Import Include="System.Data" />
    <Import Include="System.Drawing" />
    <Import Include="System.Diagnostics" />
    <Import Include="System.Windows.Forms" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Form1.vb">
      <SubType>Form</SubType>
    </Compile>
    <Compile Include="Form1.Designer.vb">
      <DependentUpon>Form1.vb</DependentUpon>
      <SubType>Form</SubType>
    </Compile>
    <Compile Include="My Project\AssemblyInfo.vb" />
    <Compile Include="My Project\Application.Designer.vb">
      <AutoGen>True</AutoGen>
      <DependentUpon>Application.myapp</DependentUpon>
    </Compile>
    <Compile Include="My Project\Resources.Designer.vb">
      <AutoGen>True</AutoGen>
      <DesignTime>True</DesignTime>
      <DependentUpon>Resources.resx</DependentUpon>
    </Compile>
    <Compile Include="My Project\Settings.Designer.vb">
      <AutoGen>True</AutoGen>
      <DependentUpon>Settings.settings</DependentUpon>
      <DesignTimeSharedInput>True</DesignTimeSharedInput>
    </Compile>
  </ItemGroup>
  <ItemGroup>
    <EmbeddedResource Include="My Project\Resources.resx">
      <Generator>VbMyResourcesResXFileCodeGenerator</Generator>
      <LastGenOutput>Resources.Designer.vb</LastGenOutput>
      <CustomToolNamespace>My.Resources</CustomToolNamespace>
      <SubType>Designer</SubType>
    </EmbeddedResource>
  </ItemGroup>
  <ItemGroup>
    <None Include="My Project\Application.myapp">
      <Generator>MyApplicationCodeGenerator</Generator>
      <LastGenOutput>Application.Designer.vb</LastGenOutput>
    </None>
    <None Include="My Project\Settings.settings">
      <Generator>SettingsSingleFileGenerator</Generator>
      <CustomToolNamespace>My</CustomToolNamespace>
      <LastGenOutput>Settings.Designer.vb</LastGenOutput>
    </None>
  </ItemGroup>
  <Import Project="$(MSBuildBinPath)\Microsoft.VisualBasic.targets" />
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
       Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
</Project>

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
<Compile Include="Form1.Designer.vb">
  <DependentUpon>Form1.vb</DependentUpon>
  <SubType>Form</SubType>
</Compile>
			
<Compile Include="Form1.Designer.vb" />
<Compile Include="Form1.vb">
  <SubType>Form</SubType>
</Compile>
			
XML code for .resx files
Proper code Improper code
<EmbeddedResource Include="Form1.resx">
  <DependentUpon>Form1.vb</DependentUpon>
  <SubType>Designer</SubType>
</EmbeddedResource>
			
<EmbeddedResource Include="Form1.resx">
  <SubType>Designer</SubType>
</EmbeddedResource>
			
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.