Visual C#创建和使用ActiveX组件 (2)

9. 把Visual Studio .Net的当前窗口切换到【MyControl.cs(设计)】窗口,并从【工具箱】中的【Windows窗体组件】选项卡中往设计窗体中按顺序拖入下列组件:

  一个GroupBox组件,并向此组件中再拖入,

  一个TextBox组件和一个Lable组件。

  10. 把Visual Studio .Net的当前窗口切换到【MyControl.cs】代码编辑窗口,并用下列代码替换MyControl.cs中的InitializeComponent过程,下列代码是初始化上述加入的组件:

private void InitializeComponent ( )
{
 this.groupBox1 = new System.Windows.Forms.GroupBox ( ) ;
 this.txtUserText = new System.Windows.Forms.TextBox ( ) ;
 this.label1 = new System.Windows.Forms.Label ( ) ;
 this.groupBox1.SuspendLayout ( ) ;
 this.SuspendLayout ( ) ;
 this.groupBox1.Controls.Add ( this.txtUserText ) ;
 this.groupBox1.Controls.Add ( this.label1 ) ;
 this.groupBox1.Location = new System.Drawing.Point ( 8 , 8 ) ;
 this.groupBox1.Name = "groupBox1" ;
 this.groupBox1.Size = new System.Drawing.Size ( 272 , 56 ) ;
 this.groupBox1.TabIndex = 0 ;
 this.groupBox1.TabStop = false ;
 this.groupBox1.Text = "Visual Studio .Net创建的Active X组件" ;
 this.txtUserText.Enabled = false ;
 this.txtUserText.Location = new System.Drawing.Point ( 84 , 20 ) ;
 this.txtUserText.Name = "txtUserText" ;
 this.txtUserText.Size = new System.Drawing.Size ( 180 , 21 ) ;
 this.txtUserText.TabIndex = 1 ;
 this.txtUserText.Text = "" ;
 this.label1.Location = new System.Drawing.Point ( 8 , 24 ) ;
 this.label1.Name = "label1" ;
 this.label1.Size = new System.Drawing.Size ( 66 , 16 ) ;
 this.label1.TabIndex = 0 ;
 this.label1.Text = "输入信息:" ;
 this.Controls.Add ( this.groupBox1 ) ;
 this.Name = "MyControl" ;
 this.Size = new System.Drawing.Size ( 288 , 72 ) ;
 this.groupBox1.ResumeLayout ( false ) ;
 this.ResumeLayout ( false ) ;
}

  至此【ActiveXDotNet】项目创建的Active X组件的界面就基本完成了,具体如图03所示:

Visual C#创建和使用ActiveX组件
图03:【ActiveXDotNet】项目创建的Active X组件的设计界面

  11. 在MyControl.cs中的【MyControl 的摘要说明】代码区中添加下列代码,以下代码是定义一个公用的接口,此接口是告诉COM/COM+,这儿有一个公用的属性可以进行读写操作:

public interface AxMyControl
{
 String UserText { set ; get ; }
}

  12. 在MyControl.cs的【MyControl】class代码区中添加下列代码,以下代码是首先定义一个私有的字符串,用此字符串来保存从Web测试页面中传递来的数值定义一个公用属性,在接下来的Web测试页面中,将通过这个属性来传递数值,此属性是可读写:

private String mStr_UserText ;
public String UserText
{
 get { return mStr_UserText ; }
 set
 {
  mStr_UserText = value ;
  //修改组件的数值
  txtUserText.Text = value ;
 }
}

  13. 保存上面的修改步骤,至此我们就利用Visual C#创建了一个名称为MyControl的class,这也就是用Visual C#封装的、酷似Active X组件的组件。

  14. 单击快捷键【Ctrl+F5】,则Visual C#会自动完成编译,并在"C:/Class/ActiveXDotNet/bin/Debug"目录生成一个名称为"ActiveXDotNet.dll"文件,这就是产生的组件。

  以下是经过上述步骤产生的MyControl.cs的全部代码:

using System ;
using System.Collections ;
using System.ComponentModel ;
using System.Drawing ;
using System.Data ;
using System.Windows.Forms ;
namespace ActiveXDotNet
{
 public interface AxMyControl
 {
  String UserText { set ; get ; }
 }
 /// <summary>
 /// MyControl 的摘要说明。
 /// </summary>
 public class MyControl : System.Windows.Forms.UserControl , AxMyControl
 {
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null ;
  private System.Windows.Forms.GroupBox groupBox1 ;
  private System.Windows.Forms.Label label1 ;
  private System.Windows.Forms.TextBox txtUserText ;
  private String mStr_UserText ;
  public String UserText
  {
   get { return mStr_UserText ; }
   set
   {
    mStr_UserText = value ;
    //修改组件的数值
    txtUserText.Text = value ;
   }
  }
  public MyControl ( )
  {
   // 该调用是 Windows.Forms 窗体设计器所必需的。
   InitializeComponent ( ) ;

   // TODO: 在 InitializeComponent 调用后添加任何初始化
  }
  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose ( bool disposing )
  {
   if ( disposing )
   {
    if ( components != null )
    {
     components.Dispose ( ) ;
    }
   }
   base.Dispose ( disposing ) ;
  }
  #region 组件设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器
  /// 修改此方法的内容。
  /// </summary>
  private void InitializeComponent ( )
  {
   this.groupBox1 = new System.Windows.Forms.GroupBox ( ) ;
   this.txtUserText = new System.Windows.Forms.TextBox ( ) ;
   this.label1 = new System.Windows.Forms.Label ( ) ;
   this.groupBox1.SuspendLayout ( ) ;
   this.SuspendLayout ( ) ;
   this.groupBox1.Controls.Add ( this.txtUserText ) ;
   this.groupBox1.Controls.Add ( this.label1 ) ;
   this.groupBox1.Location = new System.Drawing.Point ( 8 , 8 ) ;
   this.groupBox1.Name = "groupBox1" ;
   this.groupBox1.Size = new System.Drawing.Size ( 272 , 56 ) ;
   this.groupBox1.TabIndex = 0 ;
   this.groupBox1.TabStop = false ;
   this.groupBox1.Text = "Visual C#创建的Active X组件" ;
   this.txtUserText.Enabled = false ;
   this.txtUserText.Location = new System.Drawing.Point ( 84 , 20 ) ;
   this.txtUserText.Name = "txtUserText" ;
   this.txtUserText.Size = new System.Drawing.Size ( 180 , 21 ) ;
   this.txtUserText.TabIndex = 1 ;
   this.txtUserText.Text = "" ;
   this.label1.Location = new System.Drawing.Point ( 8 , 24 ) ;
   this.label1.Name = "label1" ;
   this.label1.Size = new System.Drawing.Size ( 66 , 16 ) ;
   this.label1.TabIndex = 0 ;
   this.label1.Text = "输入信息:" ;
   this.Controls.Add ( this.groupBox1 ) ;
   this.Name = "MyControl" ;
   this.Size = new System.Drawing.Size ( 288 , 72 ) ;
   this.groupBox1.ResumeLayout ( false ) ;
   this.ResumeLayout ( false ) ;
  }
  #endregion
 }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值