Andreas Verhamme (view profile) July 20, 2009 |
Environment: Visual Studio 2005 C#
This article is about creating ActiveX controls using a DotNet Usercontrol in Csharp. You can design all ActiveX features like: properties, methods and events.
1. Create a Usercontrol using Visual Studio (C#):
(
2. Configure the project properties:
(Full Size Image)
3. Modify the usercontrol interface:
Note : Make sur you added the EVENTS class: [ClassInterface(ClassInterfaceType.AutoDual), ComSourceInterfaces(typeof(UserControlEvents))]using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; // Add references using System.Runtime.InteropServices; using System.Reflection; using Microsoft.Win32; namespace CsharpWindowsActiveX [ProgId("CsharpWindowsActiveX.ActiveXUserControl")] public partial class ActiveXUserControl : UserControl public ActiveXUserControl() ..... |
4. Add the register/unregister section in the source code
// register COM ActiveX object [ComRegisterFunction()]
|
5. Add an ActiveX property:
// ActiveX properties (Get/Set) ////////////////////////////////////////////////////////////////// private int ptextVal; numericUpDown1.Value = ptextVal; } }
|
6. Add an ActiveX method:
// ActiveX methods/functions ////////////////////////////////////////////////////////////////// public interface ICOMCallable { int GetTextBoxValue(); } public int GetTextBoxValue()
|
7. Add an ActiveX event:
// Eventhandler interface ////////////////////////////////////////////////////////////////// public delegate void ControlEventHandler(int NumVal); public interface UserControlEvents public event ControlEventHandler OnButtonClick; private void buttonOK_Click(object sender, EventArgs e) int NumVal;
|
8. Register the ActiveX on your PC:
Register the new ActiveX on your computer using the command: RegAsm.exe CsharpWindowsActiveX.dll
9. Test your ActiveX:
Use the TSTCon32.exe tool from Visul Studio to test the ActiveX:
(Full Size Image)
Downloads