Hi
I have a application developed by C#. Now I want change it into an OCX control or a DLL for other program can call the function in it.
I am quit new on C#. Would you give me brife idea how can I do it? Any infomation will be very helpfull.
Thanks!
First, I assume that you are using Microsoft C# 2003 or 2005. If not, this will not be very helpful.
- Create a new Windows Application project
- Add a button to the project & double-click on the button
- Add this line of code to the click event:
- private void button1_Click(object sender, EventArgs e)
-
{
-
MessageBox.Show( ClassLibrary1.Class1.MyFun(1).ToString());
-
}
- Click on Save All to save the solution and files.
- Right-click on the solution and select Add, New Project, Class Library. This will be your dll.
- Add your function to the library. In my case I added MyFun:
- namespace ClassLibrary1
-
{
-
public class Class1
-
{
-
public static int MyFun(int i)
-
{
-
return i * 42;
-
}
-
}
-
}
- Right-click on the solution and select Build Solution
- You will get an error stating that the name ClassLibrary1 does not exist. However, your dll will have been built. You may want to use Explorer to see its location.
- Add a reference to your dll: right-click on References for your Windows App, select Add Reference, Browse, go up one level, then drill down ClassLibrary1/bin/Debug, select the dll, and press OK.
- Press F5 to run your app.
You can use this dll in any other project by just adding a reference to it. You do not need to include the code in the solution. It's confusing the first time, but it gets easier. Obviously, you will want to use meaningful names instead of the default WindowsApplication, ClassLibrary, MyFun, etc. HTH --Sam