Form and designer files not linking in Solution Explorer
Asked Answered
G

6

27

I can't seem to get the form and the designer files to link in my project. They look like this in the Solution Explorer.

enter image description here

I have excluded the files from the project and then tried including them back in the project, but this didn't work. Below is the designer code and a snippet of the forms code in case there is something in there.

public partial class FormPrompt
{
  private Button ButtonOk;
  private Container Components;
  private Label LabelPleaseEnter;
  private Label LabelPrompt;
  private TextBox TextBoxData;

  private void InitializeComponent()
  {
    this.LabelPleaseEnter = new Label();
    this.LabelPrompt = new Label();
    this.TextBoxData = new TextBox();
    this.ButtonOk = new Button();
    this.LabelPleaseEnter.Location = new Point(8, 0x58);
    this.LabelPleaseEnter.Size = new Size(0x48, 0x10);
    this.LabelPleaseEnter.Text = "Please enter";
    this.LabelPrompt.Location = new Point(80, 0x58);
    this.LabelPrompt.Size = new Size(0x98, 0x10);
    this.LabelPrompt.Text = "LabelPrompt";
    this.TextBoxData.Location = new Point(8, 0x80);
    this.TextBoxData.Size = new Size(0xe0, 20);
    this.TextBoxData.Text = "TextBoxData";
    this.TextBoxData.KeyDown += new KeyEventHandler(this.FormPrompt_KeyDown);
    this.ButtonOk.Location = new Point(8, 0x100);
    this.ButtonOk.Size = new Size(0xe0, 0x38);
    this.ButtonOk.Text = "Ok";
    this.ButtonOk.Click += new EventHandler(this.ButtonOk_Click);
    base.ClientSize = new Size(240, 0x13e);
    base.Controls.Add(this.TextBoxData);
    base.Controls.Add(this.ButtonOk);
    base.Controls.Add(this.LabelPrompt);
    base.Controls.Add(this.LabelPleaseEnter);
    this.Text = "WinForm";
    base.KeyDown += new KeyEventHandler(this.FormPrompt_KeyDown);
  }
}    


public partial class FormPrompt : Form
{

  internal DateTime FDateData;
  internal DateTimePicker FDatePicker;
  internal decimal FDecimalData;
  internal int FIntData;
  internal TPromptType FPromptType;
  internal string FStringData;


  public FormPrompt()
  {
    this.InitializeComponent();
    this.FDatePicker = new DateTimePicker();
    this.FDatePicker.Top = this.TextBoxData.Top;
    this.FDatePicker.Left = this.TextBoxData.Left;
    this.FDatePicker.Width = this.TextBoxData.Width;
    this.FDatePicker.Height = this.TextBoxData.Height;
    this.FDatePicker.Format = DateTimePickerFormat.Short;
    base.Controls.Add(this.FDatePicker);
  }
}
Greenes answered 24/3, 2014 at 1:59 Comment(2)
Have you checked the namespaces in both files?Varicolored
@Varicolored namespaces are the same in both files.Greenes
D
30

I've seen the same problem in Visual Studio 2008. Usually after compiling or closing and re-opening the solution the problem would fix itself. In Visual Studio 2012 I know that I have problems if I try to Add > Existing Item and choose all three files. Typically you only want to add the top level form.cs and VS will automatically include the .designer.cs and .resx files.

Disburse answered 24/3, 2014 at 2:31 Comment(3)
The second part about just adding the form.cs worked really nicely, thanks.Greenes
Had this problem in VS 2015. I found that the .designer.cs and .resx files did not associate when I added the form .cs file at the same time as other, unrelated .cs files. In order to get it to work, I had to add only the form .cs file from the existing files dialogue.Brewster
Agree with energ1ser. Adding form only worked! Thanks muchlyOarlock
B
26

Check the project (.csproj) file.

Inside the ItemGroup node, see if the .designer file is associated with the code-behind file. The XML should look something like this:

<Compile Include="FormPrompt.cs">
    <SubType>Form</SubType>
</Compile>
<Compile Include="FormPrompt.Designer.cs">
    <DependentUpon>FormPrompt.cs</DependentUpon>
</Compile>
Bourg answered 24/3, 2014 at 2:31 Comment(2)
The accepted answer did not do the trick for me. This one worked perfectly.Gooseneck
Works for VB too. <SubType>Form</SubType> went missing from every form in the .vbproj file for some reason. As a test I added it to just the first form in the list and VS picked it up and now all the forms open in designer again..Coppersmith
H
3

Check the project (.csproj)

<EmbeddedResource Include="Properties\Resources.resx">
  <Generator>ResXFileCodeGenerator</Generator> Check this
  <SubType>Designer</SubType>
  <LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>

<Compile Include="Properties\Resources.Designer.cs">
  <AutoGen>True</AutoGen> And Check this
  <DesignTime>True</DesignTime>
  <DependentUpon>Resources.resx</DependentUpon>
</Compile>
Housekeeper answered 14/1, 2019 at 11:1 Comment(0)
W
0

In VS2017 (c#) to solve it, in my case:

  1. exclude all file
  2. go to folder
  3. rename file (all 3 file .cs.Designer.cs .resx)
  4. refresh solution
  5. add to project back
Wilen answered 7/9, 2017 at 1:57 Comment(0)
H
0

include the .resx file into the project and in the properties of this file set Custom Tool as ResXFileCodeGenerator

Hassi answered 5/3, 2019 at 12:23 Comment(0)
A
0

Just re-add the item (form, control, etc.) to the project

1.- Exclude the item from the project

enter image description here

2.- Re-add the item, right click over the project then select 'Add' -> 'Existing Item..'

enter image description here

Finally select the .cs file of the item, Visual Studio will automatically load and associate the corresponding designer.cs file and the unliked issue will get fixed.

The above might work on most versions of Visual Studio, was verified in Visual Studio 2022

Auxiliary answered 27/6 at 16:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.