Results 1 to 4 of 4

Thread: Opening a Form from an existing C# DLL

  1. #1

    Thread Starter
    Lively Member smart_phil_dude1's Avatar
    Join Date
    Jun 2005
    Location
    Behind You!
    Posts
    125

    Opening a Form from an existing C# DLL

    Let's say you stored a Form in a separate DLL file created using C#, and you wanted to open up that form from your application. Here is what you can do:

    Step 1: Place the DLL file in the directory of the executable when compiled (Mine would be ...Project Name\bin\Debug)
    Step 2: In VC# go to "Project>Add Reference"
    Step 3: In the Browse tab, locate the DLL and click OK.
    Step 4: Add the following code to the top of your code:
    Code:
    using Test; //"Test" being your Dll's name
    using System.IO;
    Step 5: Now, place the following code as a function/whatever you'd like to call it. This function is what calls the form, but from my experiences, it's best to have it as a separate function and just call that function, so that you wouldn't get an error if the DLL was missing.
    Code:
            private void OpenFromDll()
            {
                Test.Form1 TestForm1 = new Test.Form1(); //"Test" being your Dll's name, "TestForm1" being whatever form you want to open from the dll
                TestForm1.Show(); //"TestForm1" being whatever form you want to open from the dll
            }
    Step 6: Now, you can finally call the form from the DLL, assuming that the DLL is in the same directory as the executable:
    Code:
                if (File.Exists(Application.StartupPath + "\\Test.dll")) //"Test.dll" Being your Dll File (This is just to make sure that the DLL is in the same directory as the executable, otherwise you'd get an error.)
                {
                    OpenFromDll();
                }
                else
                {
                    MessageBox.Show("Missing files in the install directory, try re-installing the program.", "Missing A File!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
    I just posted this to help any one that needs it, since I spent hours searching the internet and couldn't find anything that had this together, I had to consult multiple websites to figure out certain functions, and I also had to use my logic to figure things out, as I never took a course on programming before. This way, anyone can open a form from a DLL without any trouble

    Just giving back to the community that has helped me,
    Thanks,
    Phil.
    Please Help Us Save Ana or donate now by going to Oneana.com

    Visit my Website at http://www.therealfantasy.com

  2. #2
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Opening a Form from an existing C# DLL

    C# Code:
    1. using Test; //"Test" being your Dll's name
    Test, in such a case, may not always be the name of the DLL. The using directive describes the namespace which your object resides in.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  3. #3

    Thread Starter
    Lively Member smart_phil_dude1's Avatar
    Join Date
    Jun 2005
    Location
    Behind You!
    Posts
    125

    Re: Opening a Form from an existing C# DLL

    Well, I guess that's true.
    Please Help Us Save Ana or donate now by going to Oneana.com

    Visit my Website at http://www.therealfantasy.com

  4. #4
    New Member
    Join Date
    Jan 2012
    Posts
    3

    Re: Opening a Form from an existing C# DLL

    Great post. this is exactly what I was looking for. I'm new to C# and this will really do what I need to do. Thanks for taking the time to post this.

    Sharon

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width