Use the user control of C # To create ActiveX

Source: Internet
Author: User

Article: Create an ActiveX using a CSHARP usercontrol
Source: http://www.codeguru.com/csharp/.net/net_general/comcom/article.php/c16257
Author: Andreas verhamme
Translation: dezai

Http://www.dezai.cn/blog/article.asp? Id = 269

This article mainly introduces how to use the user control of DOTNET in C # To create ActiveX controls. You can design related properties, methods, and events of ActiveX.

Development Environment: Visual studio2005

1. Create a user control using Visual Studio

2. Set the assebmly attributes of the Assembly.

3. Interface Design for modifying user controls

Note: Be sure to add the following event class.

Program code 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")]
[Classinterface (classinterfacetype. autodual), comsourceinterfaces (typeof (usercontrolevents)]
Public partial class activexusercontrol: usercontrol
{
Public activexusercontrol ()
{
Initializecomponent ();
}
.....

4. Add the registration/logout function to the source code.

Program code // register com ActiveX Object
[Comregisterfunction ()]
Public static void registerclass (string key)
{
Stringbuilder skey = new stringbuilder (key );
Skey. Replace (@ "hkey_classes_root /","");
Registrykey regkey = registry. classesroot. opensubkey (skey. tostring (), true );
Registrykey CTRL = regkey. createsubkey ("control ");
CTRL. Close ();
Registrykey inprocserver32 = regkey. opensubkey ("inprocserver32", true );
Inprocserver32.setvalue ("codebase", assembly. getexecutingassembly (). codebase );
Inprocserver32.close ();
Regkey. Close ();
}

[Comunregisterfunction ()]
Public static void unregisterclass (string key)
{
Stringbuilder skey = new stringbuilder (key );
Skey. Replace (@ "hkey_classes_root /","");
Registrykey regkey = registry. classesroot. opensubkey (skey. tostring (), true );
Regkey. deletesubkey ("control", false );
Registrykey inprocserver32 = regkey. opensubkey ("inprocserver32", true );
Regkey. deletesubkey ("codebase", false );
Regkey. Close ();
}

5. Add an ActiveX Property

Program code/ActiveX properties (get/set) //////////////////////////////////////// //////////////////////////
Private int ptextval;
Public int textval
{
Get
{
Ptextval = (INT) (numericupdown1.value );
Return ptextval;
}
Set
{
Ptextval = value;
Numericupdown1.value = ptextval;
}
}

6. Add an ActiveX method.

Program code/ActiveX methods/functions ///////////////////////////////// /////////////////////////////////
Public interface icomcallable

{
Int gettextboxvalue ();
}
Public int gettextboxvalue ()
{
Int I = (INT) (numericupdown1.value );
MessageBox. Show ("ActiveX method: gettextboxvalue" + I. tostring ());
Return (I );
}

7. Add an ActiveX event

Program code // eventhandler interface ////////////////////////////////// ////////////////////////////////

Public Delegate void controleventhandler (INT numval );
[GUID ("0a415e38-372f-45fb-813b-d9558c787ea0")]
[Interfacetype (cominterfacetype. interfaceisidispatch)]

Public interface usercontrolevents
{
[Dispid (0x60020001)]
Void onbuttonclick (INT numval );
}

Public event controleventhandler onbuttonclick;

Private void buttonok_click (Object sender, eventargs E)
{
Int numval;
If (onbuttonclick! = NULL)
{
Numval = (INT) (numericupdown1.value );
Onbuttonclick (numval );
}
}

8. Register this ActiveX on a PC

Use the regasm.exe csharpwindowsactivex. dll command to register this new ActiveX control on your computer.

9. Test Your ActiveX

Use tstcon32.exe tool to test ActiveX in visul Studio

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.