Thursday, January 25, 2007

Create ActiveX Component in .Net



1. Create a new Class Library project.

2. When the project is created, add a new User Control to it, then add the controls, methods and properties you need.

3. To make it work, you need to expose the interface for the control. This is how COM/COM+ objects will know which are the properties they can use.
...
namespace ActiveXComponent
{
public interface IActiveXControl
{
string Title{ set; get ; }
...
}

public class ActiveXControl : System.Windows.Forms.UserControl, IActiveXControl
{
private string _title;
public string Title
{
get { return _title; }
set { _title = value; }
}
...
}
}

4. Compile the project, and copy the resulted .dll in your client application. Then embed the created user control in a web page.

<OBJECT id="ActiveXControl 1" name="ActiveXControl 1" classid="ActiveXComponent.dll#ActiveXComponent.ActiveXControl"></OBJECT>

5. You can make use of it's exposed methods and properties on client side. For example:

<script language = "javascript">
var myctrl1 = document.getElementById('ActiveXControl1');
myctrl1.Title = 'My first activeX component';
<script>