FYI the latest VS2022 has broken some core features of copy-paste. The one issue I know has been confirmed fixed for the next update is copying/pasting files within Solution Explorer doesn't work for NET Framework projects. Not sure if you're using NF or Net Core.
Someone piggy backed off that ticket to say they are having issues with VS not properly adding files with multiple subfiles. It sounds like the scenario you have. You can read more about it here.
Personally I've never had much luck doing what you're doing. The problem is that VS sees the .cs
file and assumes it is a regular C# file. So the file is added to the project as a source file and you don't get the "form" features you expect. Ultimately VS uses the designer type indicator to figure this out and if it is wrong then things don't work.
You didn't clarify, but if you're building a Winforms app are you using the newer csproj format? If you are then the problem will likely just go away. In the csproj format the files are generally not listed at all. You literally just copy the files into your project directory and they auto-magically appear in the solution and will be compiled correctly. Winforms supports this format.
You can get instructions here. AFAIK you can even do this with NET Framework projects by simply reverting the TargetFramework
property after the migration. At least for non-web projects it works. The only issue you may run into is if you're still using package.config
for Nuget references then you'd have to switch over to the package references being in the project file. This breaks any legacy Nuget package that requires running a transform when it is updated. Fortunately most of these aren't around anymore. It is also possible the Upgrade Assistant extension in VS can do this for you, haven't tried it myself.
If you need to stick with the verbose project file format then you need to change the form's .cs file to a Form
subtype. That means editing the project file by hand. Something like this.
<ItemGroup>
<Compile Include="MyForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="MyForm.designer.cs">
<DependentUpon>MyForm.cs</DependentUpon>
</Compile>
</ItemGroup>
Basically you add the subtype and VS will now recognize it as a form. The second include is to get the designer file to nest under the main form. After that reload the project and the problem is solved.