C# 如何将现有表单添加到新项目中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10316650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 13:20:07  来源:igfitidea点击:

How do you add an existing form to a new project?

c#winformsvisual-studio-2010

提问by P.Brian.Mackey

I have never been able to successfully add a Formfrom an existing file to a new project.

我从来没有能够成功地将Form现有文件中的 a添加到新项目中。

I read on a blog that you add just the *.cs file and the dependencies come in. Well, I tried this and the file did dragin and associate the *.designer and *.resx files. But, the Form icon does not display. Instead, the file looks like a normal *.cs file image. And, when I double click the file I get the code behind instead of the form object.

我在一个博客上读到你只添加了 *.cs 文件和依赖项。好吧,我试过这个,文件确实拖拽并关联了 *.designer 和 *.resx 文件。 但是,表单图标不显示。相反,该文件看起来像一个普通的 *.cs 文件图像。而且,当我双击该文件时,我得到的是代码而不是表单对象。

Is it possible to add existing Forms and get them properly recognized?

是否可以添加现有表单并正确识别它们?

采纳答案by P.Brian.Mackey

After some more research I discovered the source of the issue. It is correct that all one has to import is the *.csfile. The caveat lies in the project type. If working in a Class Library project, another step must be performed.

经过更多的研究,我发现了问题的根源。正确的做法是导入*.cs文件。需要注意的是项目类型。如果在类库项目中工作,则必须执行另一个步骤。

  1. Add a reference to System.Windows.Forms.dlland System.Drawing.
  2. Import the *.csfile
  1. 添加对System.Windows.Forms.dll和的引用System.Drawing
  2. 导入*.cs文件

Notes:

笔记:

A. The files are only properly recognized after I performed these steps and then tried to open the file. Suddenly VS "wakes up" and fixes the files.

A. 只有在我执行这些步骤并尝试打开文件后才能正确识别文件。VS 突然“醒来”并修复了文件。

B. Order of the steps does not matter. If you already imported *.cs files, just fix the references.

B. 步骤的顺序无关紧要。如果您已经导入了 *.cs 文件,只需修复引用即可。

C. If one is missing other references e.g. DevExpress or other 3rd party control imports), the *.cs files will not display properly until this has been resolved.

C. 如果缺少其他引用,例如 DevExpress 或其他 3rd 方控件导入),*.cs 文件将无法正确显示,直到此问题得到解决。

回答by Mitja Bonca

You need actually 2 files: - Form1.cs - Form1.Designer.cs

您实际上需要 2 个文件: - Form1.cs - Form1.Designer.cs

Copy - paste them to your new project (just make sure there is no such form with the same nameexisting in new project)

复制 - 将它们粘贴到您的新项目中(只需确保新项目中不存在同名表单)

回答by npclaudiu

You can import an existing Form into a project. The files that need to be imported depend on the version of the Visual Studio used to create the form.

您可以将现有表单导入到项目中。需要导入的文件取决于用于创建表单的 Visual Studio 版本。

I will assume that you have two separate projects, Foo and Bar, in different solutions:

我假设您在不同的解决方案中有两个独立的项目 Foo 和 Bar:

C:\
    Projects\
        Foo\
            Foo.sln
            Foo.vcproj
            Program.cs
            Foo.cs
            Foo.Designer.cs
            Foo.Designer.resx

and

C:\
    Projects\
        Bar\
            Bar.sln
            Bar.vcproj
            Program.cs
            Bar.cs
            Bar.Designer.cs
            Bar.Designer.resx

Now, say that you need to import fhe form Foo to the project Bar. First, you need to copy all files that accompany Foo into the Bar project folder:

现在,假设您需要将表单 Foo 导入到项目 Bar。首先,您需要将 Foo 附带的所有文件复制到 Bar 项目文件夹中:

C:\
    Projects\
        Bar\
            Bar.sln
            Bar.vcproj
            Program.cs
            Bar.cs
            Bar.Designer.cs
            Bar.Designer.resx
            Foo.cs
            Foo.Designer.cs
            Foo.Designer.resx

Then, open Bar.sln in Visual Studio and right-click on the Bar project in Solution Explorer. Select [Add existing item] and select all files that you copied for the Foo form in the dialog opened. After confirming, you should see the newly imported form correctly in Solution Explorer.

然后,在 Visual Studio 中打开 Bar.sln 并右键单击解决方案资源管理器中的 Bar 项目。选择 [添加现有项目] 并在打开的对话框中选择您为 Foo 表单复制的所有文件。确认后,您应该可以在解决方案资源管理器中正确看到新导入的表单。

回答by agha

Go to solution explorer and right click on it then add existing item, here select the existing form path. Now, and this is important, on the page where you want to use existing form, you must add header file:

转到解决方案资源管理器并右键单击它然后添加现有项目,此处选择现有表单路径。现在,这很重要,在要使用现有表单的页面上,您必须添加头文件:

using "your existing project name";

回答by Matthew Radford

Sorry P.Brian.Mackey, your solution didn't work for me. It did improve it by getting VS to recognise that it was a form rather than a code file, i.e. gave it is Icon (imagine it added Form)

抱歉 P.Brian.Mackey,您的解决方案对我不起作用。它确实通过让 VS 识别出它是一个表单而不是一个代码文件来改进它,即给它的是 Icon (想象它添加了 Form)

But only way I managed to fix the issue fully was to edit the csproj file manually in a text editor. I'm not really convinced that this is a great solution and is potentially quite dangerous, especially given I made two typing mistakes and completely broke the project but it did work once I got it right. It's also hard to find the mistakes like not closing the tags properly or forgetting a file extention.

但我设法完全解决问题的唯一方法是在文本编辑器中手动编辑 csproj 文件。我并不是真的相信这是一个很好的解决方案,并且可能非常危险,特别是考虑到我犯了两个打字错误并完全破坏了项目,但一旦我做对了,它就起作用了。也很难发现错误,例如没有正确关闭标签或忘记文件扩展名。

So the project started out with these entries after adding the files via 'Add --> Existing Item' :(P.s. I'm certain you don't have to copy the files into the project folders first, just navigate to where they are stored outside the project and VS will copy them to the folder which you right clicked on. Copy in advance of course works too.)

因此,在通过“添加 --> 现有项目”添加文件后,项目从这些条目开始:(Ps 我确定您不必先将文件复制到项目文件夹中,只需导航到它们的存储位置在项目之外,VS 会将它们复制到您右键单击的文件夹中。提前复制当然也有效。)

<Compile Include="Reports\GBudget1Report.cs">
  <SubType>Form</SubType>
</Compile>
<Compile Include="Reports\GBudget1Report.Designer.cs" />
<Compile Include="Reports\GBudget2Report.cs">
  <SubType>Form</SubType>
</Compile>
<Compile Include="Reports\GBudget2Report.Designer.cs" />
<Compile Include="Reports\LASHForm.cs">
  <SubType>Form</SubType>
</Compile>

and further down in the file:

并在文件中进一步向下:

<EmbeddedResource Include="Reports\GBudget1Report.resx" />
<EmbeddedResource Include="Reports\GBudget2Report.resx" />

Comparing these with the existing forms in the project which were working correctly (other option is if you haven't got one, is to create a new form in Visual Studio and it'll give you the correct mark-up to copy) I discovered that the DependentUpon tag isn't associating the sub files to the main form code file.

将这些与项目中正常工作的现有表单进行比较(另一个选项是,如果您没有,则在 Visual Studio 中创建一个新表单,它会给您正确的标记以进行复制)我发现DependentUpon 标记没有将子文件与主表单代码文件相关联。

So I edited it by hand to match:

所以我手工编辑它以匹配:

<Compile Include="Reports\GBudget1Report.cs">
  <SubType>Form</SubType>
</Compile>
<Compile Include="Reports\GBudget1Report.Designer.cs">
   <DependentUpon>GBudget1Report.cs</DependentUpon>
</Compile>
<Compile Include="Reports\GBudget2Report.cs">
  <SubType>Form</SubType>
</Compile>
<Compile Include="Reports\GBudget2Report.Designer.cs">
   <DependentUpon>GBudget2Report.cs</DependentUpon>
</Compile>

Again further down the file:

再次进一步向下文件:

<EmbeddedResource Include="Reports\GBudget1Report.resx">
  <DependentUpon>GBudget1Report.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Reports\GBudget2Report.resx">
  <DependentUpon>GBudget2Report.cs</DependentUpon>
</EmbeddedResource>

It's not a solution I like, but it works. If anyone has any better solutions, I'm happy to listen.

这不是我喜欢的解决方案,但它有效。如果有人有更好的解决方案,我很乐意倾听。

回答by Lord of Scripts

If you want to create a library of windows forms it is better if you create a Windows Forms Application project. Then delete the default form (Form1), go to the project properties and change the Output Type (or project type) from Windows Forms Application to Class Library.

如果要创建 Windows 窗体库,最好创建一个 Windows 窗体应用程序项目。然后删除默认表单(Form1),转到项目属性并将输出类型(或项目类型)从 Windows 窗体应用程序更改为类库。

This way the output will be a DLL but it will have the references you need for a windows forms project. As you pointed out, when adding existing items do NOT add their corresponding .Designer and .resx files, just add their top level/main file.

这样,输出将是一个 DLL,但它将具有 Windows 窗体项目所需的引用。正如您所指出的,添加现有项目时不要添加相应的 .Designer 和 .resx 文件,只需添加它们的顶级/主文件。

回答by brian

In my case, I have also noted that I can only ad one form at a time. You will get to see the forms as seperate items if you try to add multiple forms at the same time.

就我而言,我还注意到我一次只能投放一种表格。如果您尝试同时添加多个表单,您将看到这些表单是单独的项目。

回答by Harald Coppoolse

Maybe it is because of using visual studio 2012, but all these solutions didn't work. AtConway gave a hint to edit the csproj file. And that hint worked.

也许是因为使用了visual studio 2012,但所有这些解决方案都不起作用。AtConway 给出了编辑 csproj 文件的提示。这个提示奏效了。

  1. Open the solution with the project that you want to add your three files.
  2. Let's assume you want to add MyForm.cs / MyForm.Designer.cs and MyForm.resX. Make sure they are in the folder of your project
  3. Add a dummy form (or usercontrol) with a dummy name: MyTempForm.
  4. Save the solution and close it in Developer Studio
  5. In a windows explorer delete the three MyTempForm files
  6. Rename your three MyForm files in MyTempForm, with the same extensions.
  7. Open your solution again in Developer Studio 2012
  8. See that your MyForm is now fully available as MyTempForm
  9. In the solution explorer rename MyTempForm back to MyForm
  1. 打开包含要添加三个文件的项目的解决方案。
  2. 假设您要添加 MyForm.cs / MyForm.Designer.cs 和 MyForm.resX。确保它们在您的项目文件夹中
  3. 添加一个具有虚拟名称的虚拟表单(或用户控件):MyTempForm。
  4. 保存解决方案并在 Developer Studio 中关闭它
  5. 在 Windows 资源管理器中删除三个 MyTempForm 文件
  6. 使用相同的扩展名重命名 MyTempForm 中的三个 MyForm 文件。
  7. 在 Developer Studio 2012 中再次打开您的解决方案
  8. 查看您的 MyForm 现在完全可用作为 MyTempForm
  9. 在解决方案资源管理器中,将 MyTempForm 重命名回 MyForm

回答by FTheGodfather

All you have to is to pass the .cs file. (The code file)

您所要做的就是传递 .cs 文件。(代码文件)

回答by ankur

Assume that you want to import a Form called YourFormName. Then you should only add YourFormName.cs to the project - the rest parts (YourFormName.designer.cs and YourFormName.resx) will be added automatically.

假设您要导入名为 YourFormName 的表单。那么您应该只将 YourFormName.cs 添加到项目中 - 其余部分(YourFormName.designer.cs 和 YourFormName.resx)将自动添加。

steps:

脚步:

Create WinForms project (in my case .Net Framework is 4.5.1) in VS2013 Right-click on projects -> Add -> Existing Item... copy winform you want to import to the folder of your project. If you want to add it to a new folder then first create a folder then add. Search for any WinForm with controls (I added two forms created in VS2010 for .NET framework 4) in lesser than 10 sec visual studio added all the remaining files and i can open this in design mode.

在 VS2013 中创建 WinForms 项目(在我的情况下 .Net Framework 是 4.5.1)右键单击项目 -> 添加 -> 现有项目...将要导入的 winform 复制到项目的文件夹中。如果要将其添加到新文件夹,请先创建一个文件夹,然后添加。在不到 10 秒的时间内搜索任何带有控件的 WinForm(我添加了两个在 VS2010 中为 .NET framework 4 创建的表单)visual studio 添加了所有剩余的文件,我可以在设计模式下打开它。