问题
I am trying to create custom control for SAP b1 using ActiveX.
- I created Windows Forms Control Library
- Made Project Assembly Info COM-Visible (Project properties => Application => Assembly Information)
- Registerd for COM interop (Project properties => Build)
My UserControl looks like this:
[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);
}
}
Interfaces that I'm using:
[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 Addon code:
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;
};
But when I am using this Control in my SAP b1 Addon I am getting error:
{"Unable to cast object of type 'System.__ComObject' to type MyComLib.Controls.OnCheckBoxClickEventHandler'."}
If there is some good sample which describes how to export dll to activeX filetype
also how to use ComEventInterface attribute and implementation of it would be good.
[ComEventInterface(typeof(ITextBoxCheckEvents), typeof(ITextBoxCheckEvents_EventProvider))]
also if there is some good documentation how to create interop.MSComctlLib like ActiveX library would be good.
checked this blogposts, but none of them describes how to handle events
- https://blogs.sap.com/2010/01/20/how-to-create-activex-controls-with-net-part-1/
- https://www.codeguru.com/csharp/.net/net_general/comcom/article.php/c16257/Create-an-ActiveX-using-a-Csharp-Usercontrol.htm
来源:https://stackoverflow.com/questions/61060831/create-custom-activex-controls-for-sap-b1