首页 > 解决方案 > 为 SAP B1 创建自定义 ActiveX 控件

问题描述

我正在尝试使用 ActiveX 为 SAP b1 创建自定义控件。

  1. 我创建了 Windows 窗体控件库
  2. 使项目装配信息 COM 可见(项目属性 => 应用程序 => 装配信息)
  3. 注册 COM 互操作(项目属性 => 构建)

我的用户控件看起来像这样:

[ComVisible(false)]
public delegate void OnCheckBoxClickEventHandler(string val);

[ProgId("MyComLib.Controls.TextBoxCheck")]
[ClassInterface(ClassInterfaceType.None)]
[ComSourceInterfaces(typeof(ITextBoxCheckEvents))]
public partial class TextBoxCheckClass : UserControl, TextBoxCheck
{
    public string PlaceHolder
    {
        get
        {
            return textBox1.Text;
        }
        set
        {
            textBox1.Text = value;
        }
    }

    public TextBoxCheckClass()
    {
        InitializeComponent();
    }

    public event OnCheckBoxClickEventHandler OnCheckBoxClick;

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        textBox1.ReadOnly = checkBox1.Checked;
        OnCheckBoxClick?.Invoke(textBox1.Text);
    }

    private void TextBoxCheck_Load(object sender, EventArgs e)
    {
        textBox1.Text = PlaceHolder;
    }

    [ComRegisterFunction()]
    private static void RegisterClass(string key)
    {
        Registrar.RegisterClass(key, "MyComLib.Controls.TextBoxCheck");
    }

    [ComUnregisterFunction()]
    private static void UnregisterClass(string key)
    {
        Registrar.UnregisterClass(key);
    }
}

我正在使用的接口:

[Guid("5710FC13-103E-48F4-B674-80DDD3ABA0DB")]
public interface TextBoxCheck : ITextBoxCheck, ITextBoxCheckEvents_Event
{
}

[Guid("3AF22B4A-A941-4DBF-8ED5-EB6DAFF538E5")]
public interface ITextBoxCheck
{
    [DispId(1)]
    string PlaceHolder { get; set; }
}

[Guid("64C6CEC1-B855-4B7C-B2C8-31F7879DEA4E")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ITextBoxCheckEvents
{
    [DispId(1)]
    void OnCheckBoxClick(string val);
}

//[ComEventInterface(typeof(ITextBoxCheckEvents), typeof(ITreeViewEvents_EventProvider))]
[Guid("3DB40F69-E503-4CE2-8696-0349CC41114B")]
public interface ITextBoxCheckEvents_Event
{
    [DispId(1)]
    event OnCheckBoxClickEventHandler OnCheckBoxClick;
}

SAP b1 插件代码:

var classId = "MyComLib.Controls.TextBoxCheck";
var newItem = form.Items.Add("ActiveX1Id", BoFormItemTypes.it_ACTIVE_X);
var activeXControl = newItem.Specific as ActiveX;
activeXControl.ClassID = classId;

var myItem1 = activeXControl.Object as TextBoxCheck;
myItem1.PlaceHolder = "test1";

//getting error on this line
myItem1.OnCheckBoxClick += (val) =>
{
//some code;
};

但是当我在我的 SAP b1 插件中使用这个控件时,我得到了错误:

{“无法将 'System.__ComObject' 类型的对象转换为 MyComLib.Controls.OnCheckBoxClickEventHandler'。”}

如果有一些好的示例描述了如何将 dll 导出为 activeX 文件类型
,以及如何使用 ComEventInterface 属性和实现它会很好。
[ComEventInterface(typeof(ITextBoxCheckEvents), typeof(ITextBoxCheckEvents_EventProvider))]

另外,如果有一些好的文档如何创建像 ActiveX 库这样的 interop.MSComctlLib 会很好。

检查了这篇博文,但没有一篇描述如何处理事件

标签: c#comactivexadd-onsapb1

解决方案


推荐阅读