Adding an Existing Windows Form to Another Project

Adding an Existing Windows Form to Another Project

Adding an existing Windows Form in VS.Net 2003 means adding two files?for example, a MyForm.cs file with C# code and an XML-based MyForm.resx assembly resource file. In VS.Net 2005, there is a third MyForm.Designer.cs file with the partial class that includes the Windows Form Designer generated code. To add it, open the target solution in VS.NET, right click the project name in the Solution Explorer window, select Add and then Add Existing Item in the context menu, select the above mentioned files in the Add Existing Item dialog box and click the Open button. Generally, you can select only the MyForm.cs file. The selected and associated files are copied to the target project directory. If you prefer to add just the links instead of the separate files, click the drop-down button on right side of the Open button and select Link File.

If your target project has references to these two .NET assemblies: System.Windows.Forms.dll and System.Drawing.dll, as it takes place for a Windows Application project or a C# Class Library that already has a Windows Form, the VS.NET Designer knows that the added file is a Windows Form, opens it, and you won’t have problems working with it.

However, if your target is a C# Class Library that does not have the two above-mentioned references, after you double click the MyForm.cs file name in the Solution Explorer window, the file is opened with the C# Editor instead of VS.NET Designer. You may also notice that the Windows Form file icon for this file is substituted with the Class file icon.

See also  Comparing different methods of testing your Infrastructure-as-Code

The added Windows Form is shown in the C# Editor instead of the VS.NET Designer because this is default VS.NET behavior when the target .CSPROJ file is updated on the base of limited information available in this circumstance. That means that the value of the SubType attribute (VS.NET 2003) or the content of the SubType tag (VS.NET 2005) for the file in the target .CSPROJ project file will not be set to “Form”?which is required to recognize the correspondent file as a Windows Form.

An analysis of different scenarios of adding an existing Windows Forms to other projects brings us to the following rule: make sure that references to the System.Windows.Forms.dll and System.Drawing.dll assemblies exist in your target project before adding an existing Windows Form. VS.NET recognizes that the file you want to add is a Windows Form and accordingly updates the target .CSPROJ project file, thus eliminating the problem.


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist