Is it possible to create a Windows Form in a C# Class Library?

前端 未结 4 1726
栀梦
栀梦 2021-02-05 19:19

I\'ve been building DLL class libraries in C#, used as add-ons to an application which provides a Custom API. Up until now they\'ve included mostly interfacing with databases,

相关标签:
4条回答
  • 2021-02-05 19:50

    Yes, you can. You need to add a reference to System.Windows.Forms in the class library project (right-click on project -> Add -> Reference)

    0 讨论(0)
  • 2021-02-05 19:56

    You can definitely use Windows Forms inside your class library. There are few scenarios:

    1. You are adding a form to the library using FormDesigner. You can right click on project name, click on Add and then in Windows form. It should add required References for your form

    2. You are copying a form from other project. In this case the Visual Studio will not be able to identify the form and will show it as simple C# source file. In this case right click on References under the project in Visual Studio. Click on Add References and select Framework from left pane. Select System.Windows.Form and System.Drawing and click Ok. This should make the form understandable to Visual Studio and you can design it edit it using Form Designer.

    3. If you are creating form using code, add the references to required assemblies as mentioned in the step 2. You should be able to use System.Windows.Form in your project with using System.Windows.Form; statement.
    0 讨论(0)
  • 2021-02-05 20:01

    if the New form is already in a Windows project that has to stay windows application..

    try these Steps not sure if this is what you want... hope it helps.

    1. Create new Dll Project
    2. Copy Form.cs, FormDesigner.cs and form.resx into the new dll project Folder
    3. Add Existing
    0 讨论(0)
  • 2021-02-05 20:06

    What I find works best for me is to create a new Windows Forms project, and then go to the project properties and change it to a class library. This way, you can right click on folders in the solution explorer and all the WinForms items appear just as if it were still a WinForms project, but it's a class library. This also works with WPF applications.

    0 讨论(0)
提交回复
热议问题