Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A DLL with WinForms that can be launched from A main app

Tags:

c#

winforms

dll

I have created a C# DLL that has some forms in it. ( I needed it to be a DLL, not a Windows Application.) How can I run it as a Windows App? Should I create another app and load it? How? What do I need to learn to do that? please let me know if I should explain more about my question.

like image 849
Bohn Avatar asked May 12 '10 19:05

Bohn


People also ask

How to create a Windows Forms application using DLL file?

Using DLL File. Step 1 - Open Visual Studio then select "File" -> "New" -> "Project..." then select "Visual C#" -> "Windows Forms application". Step 2 - Design the form as in the following image: Step 3 - Add a reference for the dll file, "calculation.dll", that we created earlier. Right-click on the project and then click on "Add reference".

What is a WinForm library file?

A library file is .dll file, dll for dynamic link library. Lots of developers add Windows forms (WinForms) to the .exe for the user interface. However, WinForms can be added to dlls to help break up a bigger program into more manageable chunks. For this tutorial a Windows .exe is going to load a WinForm from a DLL.

How to load DLL form from a button?

Reference the form in the DLL, firstly with using DLLForm, and code to load the DLL form from a button. The code will look similar to this: Run the code and press the button to load the form from the DLL.

Where can I find the DLL form test?

In the binary output directories for the solution, under the bin folders there will be DLLFormTest.exe and DLLForm.dll, with MyForm in the .dll file. WinForms can be displayed so that no other form in the app can be used until the new form is closed.


1 Answers

If you're using VS 2008:

First, create a Windows Forms Application project. You can delete the default form that's created (Form1.cs) if you don't plan to use it.

In the Solution Explorer, right-click on the References and select Add Reference. This is the point where you add your custom designed C# DLL.

Now open Program.cs, and in make the following change:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using ****your DLL namespace here****
namespace WindowsFormsApplication2
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new [****your startup form (from the DLL) here****]);
        }
    }
}

If the DLL contains disconnected forms, you'll probably need to add a class in the winforms project to coordinate the forms behavior.

like image 60
code4life Avatar answered Oct 27 '22 00:10

code4life