Click here to Skip to main content
13,868,320 members
Click here to Skip to main content
Add your own
alternative version


54 bookmarked
Posted 3 Aug 2004

Importing and Extending ActiveX Controls in .NET

, 3 Aug 2004
Rate this:
Please Sign up or sign in to vote.
Importing and extending ActiveX controls in .NET.


We often need to reuse and extend an ActiveX control developed by other vendors in real .NET applications. There are articles and user documents on how to import an ActiveX control into Visual Studio .NET, but we don’t see much on how to customize an imported ActiveX control in the .NET environment, especially with the C# code generated by the utility AxImp.exe.

In this article, we present step-by-step instructions on how to import and extend an ActiveX control in the .NET environment. We use Microsoft Calendar Control as our example. The sample code shows a customized Calendar Control that allows user to create daily note for each day; when a day is selected, a note will show in a box like a tooltip.

Importing ActiveX Control in Visual Studio .NET

Importing an ActiveX control inside Visual Studio .NET is easy, you can just simply place your mouse cursor inside Toolbox window, right click the mouse, then you see the “Customize Toolbox” window, in this window, select the “COM components” tab, then select the ActiveX control you want to import. Here is the window with a selection of Calendar Control:

For the imported Calendar Control, Studio will create a .NET “wrapper” with a type of AXMSACAL.AxCalendar. It will also generate AxInterop.MSACAL.dll and Interop.MSACAL.dll in bin/Debug (by default) directory.

To customize the Calendar Control, you use AXMSACAL.AxCalendar as your base class and override certain methods in AXMSACAL.AxCalendar and add your own methods:

public class MyCalendar : AXMSACAL.AxCalendar
  //customize it here

You build MyCalendar into a WinForm control, add it into Visual Studio .NET toolbox, you are confident it will work, but you end up with the following error message:

This problem is due to: “by default, the ToolboxItem attribute is set to false in the generated Ax wrapper. A false value for this attribute prevents the control or wrapper from being added to the Toolbox.” (See this link.)

When we try to customize the Calendar Control in this way, we run into lots of problems mainly due to the fact that the .NET generated class AxCalendar does not expose its members in a level that its subclass can access. The reason is probably that Visual Studio .NET expects you to import and use an existing ActiveX control, but not extend an ActiveX control. Fortunately, Visual Studio .NET 2003 provides a utility called AxImp.exe that supports ActiveX importing and source code generation.

Importing ActiveX Control using AxImp.exe

To import the Calendar Control using AxImp.exe with source code generation, you can use a command like:

AxImp /source activex_control_path_name

The following screen shows the importing of Calendar Control in command line and its output:

As you can see from the above screen, two DLLs and a C# source code are generated.

The next step is to create a Windows Control Library project with Visual Studio .NET and add AxMSACAL.cs into the project. You need to copy the AxMSACAL.cs MSACAL.dll into the project directory, we don’t need the AxMSACAL.dll since we will build a customized DLL of our own.

By default, AxImp generates AxMSACAL.cs like this:

[assembly: System.Reflection.AssemblyVersion("")]

  System.Windows.Forms.AxHost.TypeLibraryTimeStamp("6/26/1998 12:00:00 AM")]

namespace AxMSACAL {





    public class AxCalendar : System.Windows.Forms.AxHost {

        private MSACAL.ICalendar ocx;

        private AxCalendarEventMulticaster eventMulticaster;

        private System.Windows.Forms.AxHost.ConnectionPointCookie cookie;

        // other stuff ommited



In order to build this control into a DLL, you need to do the following:

  1. Add MSACAL.dll and Stdole.dll as references.
  2. Comment out the [assembly: AssemblyVersion()] in AxMSACAL.cs.
  3. Add attribute [System.ComponentModel.ToolboxItem(true)] before the class AxCalendar.
    public class AxCalendar : System.Windows.Forms.AxHost
  4. Change the default namespace “AxMSACAL” into whatever namespace your project defines.

Extending ActiveX Control

To customize the Calendar Control, you need to do the following:

  1. Create a new class called MyCalendar extending AxCalendar.
    public class MyCalendar : AxCalendar
  2. Modify the base class of AxCalendar, if necessary, to make subclass MyCalendar easier to implement. For the Calendar Control, we have to at least change certain members’ visibility modifier in AxCalender from private to protected so subclass can access them:
    public class AxCalendar : System.Windows.Forms.AxHost 
        protected MSACAL.ICalendar ocx;
        protected AxCalendarEventMulticaster eventMulticaster;
        protected System.Windows.Forms.AxHost.ConnectionPointCookie cookie;
        // other stuff
  3. Implement subclass MyCalendar and add your own stuff.


In this article, we showed how to use AxImp to import an ActiveX control with C# code generation, then customize the control with the generated code and extend it.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Colin Peng & Yang Si
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionCould you import an ActiveX and rewrite it in .NET maintaining binary compatibility? Pin
Giorgio Malagutti27-Apr-10 3:37
memberGiorgio Malagutti27-Apr-10 3:37 
Questioncreate OCX Pin
rafeek_monsef21-Apr-07 22:35
memberrafeek_monsef21-Apr-07 22:35 
GeneralCLIPSOCX in c# Pin
sa maq21-Dec-05 0:04
membersa maq21-Dec-05 0:04 
GeneralURI Pin
the niros18-Jan-05 5:22
memberthe niros18-Jan-05 5:22 
GeneralPopulating the days with text on load Pin
brent_a_kendall7-Sep-04 6:44
memberbrent_a_kendall7-Sep-04 6:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190214.1 | Last Updated 4 Aug 2004
Article Copyright 2004 by Colin Peng & Yang Si
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid