-
Jun 2nd, 2007, 12:05 PM
#1
Thread Starter
Lively Member
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.
-
Jun 3rd, 2007, 08:25 PM
#2
Re: Opening a Form from an existing C# DLL
C# Code:
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
-
Jun 4th, 2007, 04:52 PM
#3
Thread Starter
Lively Member
Re: Opening a Form from an existing C# DLL
Well, I guess that's true.
-
Jan 27th, 2012, 09:56 AM
#4
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|