none
COM ActiveX to .NET Interop - Design-time issues!! RRS feed

  • Question

  • I have a Visual Basic 6.0 ActiveX Control that I am trying to import into .NET. 

     

    I have created a project that wraps up the ActiveX Control.  The ActiveX control is wrapped by a C# class that inherits from the "UserControl" class.

     

    I then try to add the .NET control (UserControl) that wraps this ActiveX onto a form in another project.  

     

    The problem that I encounter is that when I add the wrapped control to the form it starts up in runtime, instead of design-time.  What I want is the wrapped control to show "Design-time" view when inside .NET design-time environment, and "Run-time" when inside the .NET run-time environment.

     

    The ActiveX is properly designed to check the Ambient.UserMode property - and determine if it is in design/run time mode.  However, it is being told by the wrapper (or .NET IDE, CLR, ...?) that it is in run-time mode when it is sited on the .NET UserForm during design-time.

     

    I have been looking for some time and can't find a thing...   Anyone provide a little help?!?!!   Is there some secret property?

    Tuesday, March 25, 2008 3:29 PM

Answers

  • I determined a solution.

     

    First, the "problem" was due to this piece of code in the InitializeComponent function

    Code Snippet
    this.axMyActiveX1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axMyActiveX1.OcxState")));

    This is what was putting my ActiveX into run-time mode.

     

    I tried removing that line from InitializeComponent but .NET complained and refused to show me my UserControl form anymore.  .NET also had big hiccups when I tried modifying the code in InitializeComponent to only set this when the UserControl was in run-time mode.

     

    Therefore, I added code into the Constructor of my UserControl - to check if we were in run-time or design-time mode and then call the appropriate function.

    Code Snippet

    // use the appropriate function to keep the ActiveX in design-time mode

    if (LicenseManager.UsageMode != LicenseUsageMode.Designtime)

    InitializeComponent();

    else

    InitializeComponentDesignTime();

    The function "InitializeComponentDesignTime" is exactly the same as "InitializeComponent" but without the previously mentioned line of code which set the ActiveX into run-time mode.

     

    So - it works for me - but is a hack.   If anyone else has a better solution please let me know.
    Friday, March 28, 2008 6:12 PM

All replies

  • It must have something to do with wrapping it into a UserControl control.   I have tried hosting the UserControl on a form within the same project and get the same behavior. 

     

    Anyone, help!?

    Wednesday, March 26, 2008 9:21 PM
  • Could you please clarify how exactly "The ActiveX control is wrapped by a C# class that inherits from the "UserControl" class."?
    Thursday, March 27, 2008 7:14 AM
  • Thank you very much for your response!

     

    I have a C# UserControl and place the ActiveX control on it.   The UserControl handles the properties of the ActiveX control and business logic to better expose the ActiveX control for use in .NET.   I have added a code sample below (I had to rename some things for sake of protecting I.P.)

     

    Code Snippet

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Drawing;

    using System.Data;

    using System.Text;

    using System.Windows.Forms;

    namespace MyProjectNamespace

    {

    public partial class myUserControl : UserControl

    {

    public myUserControl()

    {

    InitializeComponent();

    }

    public new void Show()

    {

    this.myActiveX.Show();

    }

    public string Caption

    {

    get { return this.myActiveX.ICCaption(); }

    set { this.myActiveX.ICCaption = value; }

    }

    // ... other business logic ...

    }

    }

     

    Thursday, March 27, 2008 12:35 PM
  • If you place the Microsoft Common Dialog Control (COM) on a UserControl (.NET) you will get the same behavior.  When that .NET UserControl is placed on another .NET UserForm the COM Dialog Control becomes invisible because it is in "runtime" mode within the UserControl.    

    I realize this is the typical desired behavior - just trying to figure out if there is a way to "force" the child controls to stay in Design Time mode.   In other words, to trick them into staying as design time mode when the UserControl is in design-time mode.

     

    Thursday, March 27, 2008 3:12 PM
  • I determined a solution.

     

    First, the "problem" was due to this piece of code in the InitializeComponent function

    Code Snippet
    this.axMyActiveX1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axMyActiveX1.OcxState")));

    This is what was putting my ActiveX into run-time mode.

     

    I tried removing that line from InitializeComponent but .NET complained and refused to show me my UserControl form anymore.  .NET also had big hiccups when I tried modifying the code in InitializeComponent to only set this when the UserControl was in run-time mode.

     

    Therefore, I added code into the Constructor of my UserControl - to check if we were in run-time or design-time mode and then call the appropriate function.

    Code Snippet

    // use the appropriate function to keep the ActiveX in design-time mode

    if (LicenseManager.UsageMode != LicenseUsageMode.Designtime)

    InitializeComponent();

    else

    InitializeComponentDesignTime();

    The function "InitializeComponentDesignTime" is exactly the same as "InitializeComponent" but without the previously mentioned line of code which set the ActiveX into run-time mode.

     

    So - it works for me - but is a hack.   If anyone else has a better solution please let me know.
    Friday, March 28, 2008 6:12 PM