加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何在运行时添加ActiveX控件

发布时间:2020-12-15 17:28:59 所属栏目:百科 来源:网络整理
导读:我试图在基于C#窗体表单的项目中的用户控件中添加activeX控件. 现在,如果我从工具菜单中添加activeX组件,那么只需使用拖放即可使用activeX控件. 但是当我尝试使用C#代码在运行时添加那个时,它会抛出以下异常: “Exception of Type ‘System.Windows.Forms.A
我试图在基于C#窗体表单的项目中的用户控件中添加activeX控件.

现在,如果我从工具菜单中添加activeX组件,那么只需使用拖放即可使用activeX控件.
但是当我尝试使用C#代码在运行时添加那个时,它会抛出以下异常:

“Exception of Type
‘System.Windows.Forms.AxHost=InvalidActiveXStateException’ was
thrown”.

使用CreateControl()我能够摆脱这个异常,但现在activeX控件没有出现在窗体上.

解决方法

您何时添加控件以及在表单中添加控件的位置?

您通常会在组件初始化之后在构造函数中加载控件:

public FormRecalculation()
    {
        InitializeComponent();
        loadDataSelector();
    }

如果有任何关联的许可证密钥,您需要设置它们并将它们添加到表单上的相应容器中:

private void loadDataSelector()
    {
        //Initialize the DataSelector
        DataSelector = new AXQDataSelector(getClsidFromProgId("QDataSelLib.QDataSel"));
        if (DataSelector != null)
        {
            System.Reflection.FieldInfo f =
                typeof(AxHost).GetField("licenseKey",System.Reflection.BindingFlags.NonPublic |
                System.Reflection.BindingFlags.Instance);
            f.SetValue(DataSelector,"license-here");

            splitContainer1.Panel2.Controls.Add(DataSelector);

            ((System.ComponentModel.ISupportInitialize)(DataSelector)).BeginInit();

            this.DataSelector.Dock = System.Windows.Forms.DockStyle.Fill;
            this.DataSelector.Enabled = true;
            this.DataSelector.Location = new System.Drawing.Point(0,0);
            this.DataSelector.Name = "DataSelector";
            this.DataSelector.Size = new System.Drawing.Size(324,773);
            this.DataSelector.TabIndex = 0;

            splitContainer1.Panel2.ResumeLayout();
            ((System.ComponentModel.ISupportInitialize)(DataSelector)).EndInit();

            this.ResumeLayout(false);
            this.PerformLayout();
        }
        else
        {
            return;
        }

    }

这实际上是为了包装的OCX,但你明白了……

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读