首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

C#编写ActiveX控件有关问题。IE中无法触发事件

2012-01-19 
C#编写ActiveX控件问题。IE中无法触发事件C#编写的ActiveX控件中含有事件。在HTML中无法触发,始终提示事件为

C#编写ActiveX控件问题。IE中无法触发事件
C#   编写的   ActiveX   控件中含有事件。在HTML中无法触发,始终提示   事件为空!
HTML代码如下
<html>
<body   bgcolor=’#223344’>

<Script   Language=JavaScript>
function   test()
  {
helloworld.TestVoid( "test11 ");
}
</Script>
<object   id= "helloworld "    
      classid= "clsid:A69AC4D2-7A3E-45f8-93D5-84F39E302116 "   Width= "184 "   Height= "96 "            
>      
</object>

<script   for= "helloworld "   event= "OnTestEvent(msg) "   language= "javascript ">
  alert(msg);
</script>


<input   type= "button "   onclick= "test() "   value=’Click’>    
</body>
</html>

AceiveX源码
    public   delegate   void   TestEvent(string   msg);
        [Guid( "A69AC4D2-7A3E-45f8-93D5-84F39E302116 ")]
        [ComVisible(true)]
        [ClassInterface(ClassInterfaceType.AutoDual)]
        [ComSourceInterfaces(typeof(TestOcx.IUserControl1))]
        public   class   UserControl1   :   UserControl,   IObjectSafety,   TestOcx.IUserControl1
        {            
              public   event   TestEvent   OnTestEvent;
                ///   <summary>
                ///   必需的设计器变量。
                ///   </summary>
                private   System.ComponentModel.IContainer   components   =   null;

                ///   <summary>
                ///   清理所有正在使用的资源。
                ///   </summary>
                ///   <param   name= "disposing "> 如果应释放托管资源,为   true;否则为   false。 </param>
                protected   override   void   Dispose(bool   disposing)
                {
                        if   (disposing   &&   (components   !=   null))
                        {
                                components.Dispose();
                        }
                        base.Dispose(disposing);
                }

                #region   组件设计器生成的代码

                ///   <summary>
                ///   设计器支持所需的方法   -   不要


                ///   使用代码编辑器修改此方法的内容。
                ///   </summary>
                private   void   InitializeComponent()
                {
                        this.label1   =   new   System.Windows.Forms.Label();
                        this.SuspendLayout();
                        //  
                        //   label1
                        //  
                        this.label1.AutoSize   =   true;
                        this.label1.Location   =   new   System.Drawing.Point(20,   31);
                        this.label1.Name   =   "label1 ";
                        this.label1.Size   =   new   System.Drawing.Size(89,   12);
                        this.label1.TabIndex   =   0;
                        this.label1.Text   =   "一个简单的控件 ";
                        //  
                        //   UserControl1
                        //  
                        this.AutoScaleDimensions   =   new   System.Drawing.SizeF(6F,   12F);
                        this.AutoScaleMode   =   System.Windows.Forms.AutoScaleMode.Font;
                        this.Controls.Add(this.label1);
                        this.Name   =   "UserControl1 ";
                        this.Size   =   new   System.Drawing.Size(133,   78);
                        this.ResumeLayout(false);
                        this.PerformLayout();

                }

                #endregion

                private   System.Windows.Forms.Label   label1;
                public   UserControl1()
                {
                        InitializeComponent();
                }



                public   void   TestVoid(string   msg)
                {
                        MessageBox.Show( "TestVoid: "+msg);
                       
                        //发生事件
                        if   (this.OnTestEvent   !=   null)
                                OnTestEvent(msg);
                        else
                                MessageBox.Show( "事件为空 ");
                }



[解决办法]
namespace ClassLibrary222
{
[GuidAttribute( "9FD5FA47-A13C-4fb7-BBC2-740AB0FC0EB5 "), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]
public interface ControlEvents
{
[DispIdAttribute(1)]
void OnTestEvent(string msg);
}
/// <summary> Class1 的摘要说明 </summary>
/// <remarks> adsfasdfasdf </remarks>
[Guid( "A69AC4D2-7A3E-45f8-93D5-84F39E302116 ")]
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComSourceInterfacesAttribute(typeof(ClassLibrary222.ControlEvents))]
public class UserControl1 : UserControl
{
public delegate void TestEvent(string msg);
public event TestEvent OnTestEvent;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name= "disposing "> 如果应释放托管资源,为 true;否则为 false。 </param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region 组件设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(20, 31);
this.label1.Name = "label1 ";
this.label1.Size = new System.Drawing.Size(91, 17);
this.label1.TabIndex = 0;
this.label1.Text = "一个简单的控件 ";
//
// UserControl1
//
this.Controls.Add(this.label1);
this.Name = "UserControl1 ";
this.Size = new System.Drawing.Size(133, 78);
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Label label1;
public UserControl1()
{
InitializeComponent();
}

public void TestVoid(string msg)
{
MessageBox.Show( "808p: "+msg);

//发生事件
if (this.OnTestEvent != null)
OnTestEvent(msg);
else
MessageBox.Show( "事件为空 ");


}
}
}

热点排行