Click here to Skip to main content
Click here to Skip to main content

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.

Introduction

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("7.0.0.0")]

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

namespace AxMSACAL {

    [System.Windows.Forms.AxHost.ClsidAttribute(
                  "{8e27c92b-1264-101c-8a2f-040224009c02}")]

    [System.ComponentModel.DesignTimeVisibleAttribute(true)]

    [System.ComponentModel.DefaultEvent("AfterUpdate")]

    [System.ComponentModel.DefaultProperty("_Value")]

    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.
    [System.ComponentModel.ToolboxItem(true)]
    
    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.

Summary

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.

License

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

Share

About the Author

Colin Peng & Yang Si

United States United States
No Biography provided

Comments and Discussions

 
Questioncreate OCX Pinmemberrafeek_monsef21-Apr-07 21:35 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 4 Aug 2004
Article Copyright 2004 by Colin Peng & Yang Si
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid