简体   繁体   English

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

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

I have never been able to successfully add a Form from an existing file to a new project.我从来没有能够成功地将现有文件中的Form添加到新项目中。

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.我在博客上读到,您只添加了 *.cs 文件并且依赖项进来了。好吧,我尝试了这个,文件确实拖动并关联了 *.designer 和 *.resx 文件。 But, the Form icon does not display .但是,表单图标不显示 Instead, the file looks like a normal *.cs file image.相反,该文件看起来像一个普通的 *.cs 文件图像。 And, when I double click the file I get the code behind instead of the form object.而且,当我双击文件时,我得到的是后面的代码而不是表单对象。

Is it possible to add existing Forms and get them properly recognized?是否可以添加现有表单并正确识别它们?

Here's an approach that doesn't involve closing the project and reopening it :这是一种不涉及关闭项目并重新打开它的方法:

  1. Add the 3 existing form files (cs, Designer.cs and resx).添加 3 个现有的表单文件(cs、Designer.cs 和 resx)。
  2. Now, exclude the 3 files you just added from the project.现在,排除您刚刚从项目中添加的 3 个文件。
  3. Open the Add existing item explorer window and go to your project directory.打开 Add existing item explorer 窗口并转到您的项目目录。
  4. Select the cs file and Add.选择cs文件并添加。
  5. Tada.多田。 all good都好

After some more research I discovered the source of the issue.经过更多研究,我发现了问题的根源。 It is correct that all one has to import is the *.cs file.正确的是,只需要导入*.cs文件。 The caveat lies in the project type.需要注意的是项目类型。 If working in a Class Library project, another step must be performed.如果在类库项目中工作,则必须执行另一个步骤。

  1. Add a reference to System.Windows.Forms.dll and System.Drawing .添加对System.Windows.Forms.dllSystem.Drawing的引用。
  2. Import the *.cs file导入*.cs文件

Notes:笔记:

A. The files are only properly recognized after I performed these steps and then tried to open the file. A. 只有在我执行了这些步骤并尝试打开文件后才能正确识别这些文件。 Suddenly VS "wakes up" and fixes the files.突然VS“醒来”并修复了文件。

B. Order of the steps does not matter . B.步骤的顺序无关紧要 If you already imported *.cs files, just fix the references.如果您已经导入 *.cs 文件,只需修复引用。

C. If one is missing other references eg DevExpress or other 3rd party control imports), the *.cs files will not display properly until this has been resolved. C. 如果缺少其他引用,例如 DevExpress 或其他 3rd 方控件导入),*.cs 文件将无法正确显示,直到此问题得到解决。

Sorry P.Brian.Mackey, your solution didn't work for me.抱歉 P.Brian.Mackey,您的解决方案对我不起作用。 It did improve it by getting VS to recognise that it was a form rather than a code file, ie gave it is Icon (imagine it added Form)它确实通过让 VS 识别它是一个表单而不是一个代码文件来改进它,即给它是图标(想象它添加了表单)

But only way I managed to fix the issue fully was to edit the csproj file manually in a text editor.但我设法完全解决问题的唯一方法是在文本编辑器中手动编辑 csproj 文件。 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.也很难找到诸如未正确关闭标签或忘记文件扩展名之类的错误。

So the project started out with these entries after adding the files via 'Add --> Existing Item' :(Ps 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.如果有人有更好的解决方案,我很乐意倾听。

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.现在,假设您需要将表单 Foo 导入到项目 Bar 中。 First, you need to copy all files that accompany Foo into the Bar project folder:首先,您需要将 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.然后,在 Visual Studio 中打开 Bar.sln 并在解决方案资源管理器中右键单击 Bar 项目。 Select [Add existing item] and select all files that you copied for the Foo form in the dialog opened.选择 [添加现有项目] 并在打开的对话框中选择您为 Foo 表单复制的所有文件。 After confirming, you should see the newly imported form correctly in Solution Explorer.确认后,您应该可以在解决方案资源管理器中正确看到新导入的表单。

I've just encountered similar issues when upgrading VisualBasic forms, going from VisualStudio 2010 to VisualStudio 2013. There appear to be two ways to add existing items.我刚刚在升级 VisualBasic 表单时遇到了类似的问题,从 VisualStudio 2010 升级到 VisualStudio 2013。似乎有两种方法可以添加现有项目。

Problem: If I choose Main Window->Project->Add Existing Item and pull in only the file formname.vb, the result appears to be interpreted as code only (no designer), points to the original file (rather than taking a copy to the new project) and has other issues.问题:如果我选择 Main Window->Project->Add Existing Item 并仅拉入文件 formname.vb,结果似乎被解释为仅代码(无设计器),指向原始文件(而不是复制到新项目)并有其他问题。

Solution: If instead, I go to the Solution Explorer window, click to select the project (as opposed to the lower level objects in the tree) and then right click in the window, the resulting menu offers Add->Existing Item.解决方案:如果相反,我转到解决方案资源管理器窗口,单击以选择项目(而不是树中的较低级别对象),然后在窗口中单击鼠标右键,结果菜单提供添加->现有项目。 Using this version works as expected, requiring only that I locate the formname.vb file.使用此版本按预期工作,只需要我找到 formname.vb 文件。 No manually copying files, no pointing to mulitple files, no editing scripts, etc. I'd guess the same applies for forms written in C.没有手动复制文件,没有指向多个文件,没有编辑脚本等。我猜这同样适用于用 C 编写的表单。

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)复制 - 将它们粘贴到您的新项目中(只需确保新项目中不存在同名的此类表单)

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";

If you want to create a library of windows forms it is better if you create a Windows Forms Application project.如果您想创建一个 Windows 窗体库,最好创建一个 Windows 窗体应用程序项目。 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.然后删除默认窗体(Form1),进入项目属性,将输出类型(或项目类型)从 Windows 窗体应用程序更改为类库。

This way the output will be a DLL but it will have the references you need for a windows forms project.这样,输出将是一个 DLL,但它将包含 Windows 窗体项目所需的引用。 As you pointed out, when adding existing items do NOT add their corresponding .Designer and .resx files, just add their top level/main file.正如您所指出的,在添加现有项目时,不要添加它们对应的 .Designer 和 .resx 文件,只需添加它们的顶级/主文件。

Maybe it is because of using visual studio 2012, but all these solutions didn't work.也许是因为使用了 Visual Studio 2012,但所有这些解决方案都不起作用。 AtConway gave a hint to edit the csproj file. AtConway 提示编辑 csproj 文件。 And that hint worked.这个提示奏效了。

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

Assume that you want to import a Form called YourFormName.假设您要导入名为 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.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.在 VS2013 中创建 WinForms 项目(在我的情况下 .Net Framework 是 4.5.1)右键单击项目 -> 添加 -> 现有项...将您要导入的 winform 复制到项目的文件夹中。 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.搜索任何带有控件的 WinForm(我在 VS2010 中为 .NET 框架 4 添加了两个表单)在不到 10 秒的时间内 Visual Studio 添加了所有剩余的文件,我可以在设计模式下打开它。

After trying several methods the easiest way for me to use an existing WinForm was found to be: (similar to HaraldDuch's answer) and tested only on VS2013:在尝试了几种方法后,我发现使用现有 WinForm 的最简单方法是:(类似于 HaraldDuch 的答案)并且仅在 VS2013 上进行了测试:

1) Before moving your existing WinForm to the destination project's source folder, create a dummy WinForm using the same name of your source WinForm. 1) 在将现有 WinForm 移动到目标项目的源文件夹之前,使用与源 WinForm 相同的名称创建一个虚拟 WinForm。

2) Close your VS solution while saving. 2)保存时关闭您的VS解决方案。

3) Delete/Replace the newly created dummy set of WinForm files (*.cs, *.Designer.cs and *.resx) from Windows Explorer (off VS IDE) with the existing WinForm files you want to use. 3) 从 Windows 资源管理器(关闭 VS IDE)中删除/替换新创建的 WinForm 虚拟文件集(*.cs、*.Designer.cs 和 *.resx),并使用您要使用的现有 WinForm 文件。

4) Open VS to find your existing WinForm and you can rename it from VS IDE if you wish and you will need to change the namespace of the old WinForm to match your new project. 4) 打开 VS 找到您现有的 WinForm,如果您愿意,可以从 VS IDE 重命名它,您需要更改旧 WinForm 的命名空间以匹配您的新项目。

After doing a lot of testing and failing to correctly recognise the form when added, even though .designer and .resx were copying through when .vb was imported, I found Vidar's solution the best, least hacky fix.在进行了大量测试并且在添加时未能正确识别表单之后,即使在导入 .vb 时 .designer 和 .resx 正在复制,我发现Vidar 的解决方案是最好的,最少的 hacky 修复。

It appears that if these references are not already in the project (if there are no forms yet), the form is not recognised as a form and is imported as a code module instead.看来,如果项目中还没有这些引用(如果还没有表单),则该表单不会被识别为表单,而是作为代码模块导入。

  • System.Drawing系统绘图
  • System.Windows.Forms System.Windows.Forms

These can either be added manually per their solution above, or you can simply这些可以根据上面的解决方案手动添加,也可以简单地

  1. Add a new blank form to the project (this automatically adds the references above)向项目添加一个新的空白表单(这会自动添加上面的引用)
  2. Add existing form .vb/.cs you want to import (it will now show the form icon in the Solution Explorer and 'View Designer' will be available from context)添加要导入的现有表单 .vb/.cs(它现在将在解决方案资源管理器中显示表单图标,并且可以从上下文中使用“视图设计器”)
  3. Delete the blank form or use it for something else删除空白表格或将其用于其他用途

This does not require editing any of the VS config files as shown in other answers to the question.这不需要编辑任何 VS 配置文件,如问题的其他答案所示。

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.如果您尝试同时添加多个表单,您将看到这些表单是单独的项目。

All that is needed in visual Studio 2012 is the following: Visual Studio 2012 中所需的一切如下:

  1. Copy the three files for the form into the project folder (.cs, .designer.cs and .resx)将表单的三个文件复制到项目文件夹中(.cs、.designer.cs 和 .resx)
  2. Go to the project and add an existing object.转到项目并添加现有对象。
  3. Select the .CS file选择 .CS 文件
  4. It will import and look like a standard code file.它将导入并看起来像一个标准代码文件。 Double-clicking on it will not open the designer as mentioned above.如上所述,双击它不会打开设计器。 However just click on save all files and then close and re-open the project, now it is recognized as a form and works correctly.但是,只需单击保存所有文件,然后关闭并重新打开项目,现在它被识别为表单并且可以正常工作。

You can still use "Add Existing iTem"您仍然可以使用“添加现有 iTem”

  1. "Add Existing iTem", select three files (.cs .resx .Designer.cs) “添加现有iTem”,选择三个文件(.cs .resx .Designer.cs)

  2. make sure "namespace Application" in your .cs file is the same as the project确保 .cs 文件中的“命名空间应用程序”与项目相同

  3. now you can use auto-complete the Form you just added.现在您可以使用自动完成刚刚添加的表格。

This worked for me just now (I have the same problem as you).这刚刚对我有用(我和你有同样的问题)。 1. Created new winform with the same name, added something to it (so the resx file is created), saved and closed. 1.创建新的同名winform,添加一些东西(因此创建resx文件),保存并关闭。 2. Replace newly created files with your original form. 2. 用您原来的表格替换新创建的文件。 3. Done 3. 完成

Drag and drop .cs files from filesystem to project tree in Solution Explorer (for example):将 .cs 文件从文件系统拖放到解决方案资源管理器中的项目树(例如):

  • mainForm.cs主窗体.cs
  • mainForm.Designer.cs mainForm.Designer.cs

Don't forget about references if it is not Windows Forms project.如果它不是 Windows 窗体项目,请不要忘记引用。

Although this is an old post, I was having the exact same problem as @MatthewRadford had described above.尽管这是一篇旧帖子,但我遇到了与@MatthewRadford 上面描述的完全相同的问题。 My workaround for this was to only add the .CS file (do not add the .resx file), and allow visual studio to automatically generate the .resx file right after adding the .cs file.我的解决方法是只添加 .CS 文件(不要添加 .resx 文件),并允许 Visual Studio 在添加 .cs 文件后立即自动生成 .resx 文件。

However, I found a permanent solution .但是,我找到了一个永久的解决方案 If you began to experience this problem immediately after upgrading to a new version of visual studio, it is possible that during the upgrade, Active Reports was not registered properly.如果您在升级到新版本的 Visual Studio 后立即开始遇到此问题,则可能是在升级过程中,Active Reports 没有正确注册。 The solution is to deactivate your Active Reports (using GrapeCity License Manager), uninstall Active Reports, reinstall Active Reports from scratch, and reactivate your license.解决方案是停用您的 Active Reports(使用 GrapeCity License Manager),卸载 Active Reports,从头开始重新安装 Active Reports,然后重新激活您的许可证。 You should now be able to add the ReportName.cs and ReportName.resx, as well as the Designer.cs file, all at the same time, while having visual studio correctly handle their dependencies.您现在应该能够同时添加 ReportName.cs 和 ReportName.resx 以及 Designer.cs 文件,同时让 Visual Studio 正确处理它们的依赖关系。

只需添加表单文件(form.vb 或 form.cs),其他文件将自动创建。

The simplest solution that works for me is,对我有用的最简单的解决方案是,

  1. Close visual studio if its open如果它打开,请关闭视觉工作室

  2. Copy all the 3 files (.cs, .designer.cs, .resx) into your project folder where all the other forms reside too.将所有 3 个文件(.cs、.designer.cs、.resx)复制到所有其他表单所在的项目文件夹中。

  3. Now open the project using visual studio.现在使用 Visual Studio 打开项目。

  4. Build the project构建项目

  5. Open the form by right clicking it and selecting view designer, this will generate the proper designer.cs for the form.通过右键单击它并选择视图设计器打开表单,这将为表单生成正确的 Designer.cs。 If you don't do this sometimes it might give an error saying如果您有时不这样做,可能会出现错误提示

InitializeComponent doesnt exist int the current context.当前上下文中不存在 InitializeComponent。

  1. Now you may open and edit the .cs code file现在您可以打开并编辑 .cs 代码文件

The new form will appear like a normal form in the solution explorer here after.之后,新表单将在解决方案资源管理器中显示为正常表单。

I tried almost all of the proposed solutions but none of them really worked for me.我尝试了几乎所有提出的解决方案,但没有一个真正适合我。 I finally ended up editing the csproj file myself.我终于自己编辑了 csproj 文件。 After I added the forms from another project into my new project, I opened the csproj file and I noticed that there was a missing part to the reference that was added by Visual Studio.在我将另一个项目中的表单添加到我的新项目中后,我打开了 csproj 文件,我注意到 Visual Studio 添加的引用缺少部分。 Therefore, I edited the node according to the following template.因此,我根据以下模板编辑了节点。

<Compile Include="NewForm.cs">
  <SubType>Form</SubType>
</Compile>
<Compile Include="NewForm.Designer.cs">
  <DependentUpon>NewForm.cs</DependentUpon>
</Compile>

Then I restarted Visual Studio and everything worked like a charm.然后我重新启动了 Visual Studio,一切都很顺利。

All you have to is to pass the .cs file.您所要做的就是传递 .cs 文件。 (The code file) (代码文件)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将新项目添加到现有项目并从新项目创建 dll 文件? - How can i add a new project to my existing already project and create a dll file from the new project? 如何判断EF4实体是新实体还是现有记录? - How do you tell if an EF4 entity is new or an existing record? 如何在现有解决方案中将项目添加到源代码管理 - how do add a project to source control within an existing solution 如何将cs文件添加到现有C#项目? - How do I add a cs file to an existing C# project? 如何在XElement上放置命名空间别名? 如何将数据源添加到现有RDL文档? - How do you place namespace alias on an XElement? How do you add a datasource to existing RDL document? 如何将新的XmlElement添加到现有的XmlNodeList? - How do I add a new XmlElement to an existing XmlNodeList? 将现有表单添加到项目中导致Intellitrace崩溃 - Add Existing Form To Project Causes Intellitrace To Crash 如何按名称将现有表单动态添加到集合中 - How do I dynamically add an existing form to a collection by its name 如何将身份添加到现有项目 - How to add identity to an existing project 如何在项目 C# 中添加新的 win 表单? - How to add new win form in project C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM