13. 保存上面的修改步骤,至此我们就利用Visual C#创建了一个名称为MyControl的class,这也就是用Visual C#封装的、酷似Active X组件的组件。
14. 单击快捷键【Ctrl+F5】,则Visual C#会自动完成编译,并在"C:ClassActiveXDotNetbinDebug"目录生成一个名称为"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
}
}
数据挖掘研究院
四.Visual C#中使用刚封装的Active X组件:
数据挖掘研究院
以下步骤就是通过Web页面的方式来测试上面创建组件: 数据挖掘论坛
1. 创建一个名称为test.htm文件,MyControl就是放在此Web页面中加以测试的,此文件的内容如下:
<html>
<body color = white>
<hr> 数据挖掘实验室
<font face = arial size = 1>
<OBJECT id = "MyControl1" name = "MyControl1" classid = "ActiveXDotNet.dll#ActiveXDotNet.MyControl" width = 288 height = 72 >
</OBJECT>
</font> 数据挖掘交友
<form name = "frm" id = "frm" >
<input type = "text" name = "txt" value = "请输入数据:" ><input type = button value = "确定" onClick = "doScript ( ) ; ">
</form>
<hr>
</body>
<script language = "javascript">
function doScript ( )
{
MyControl1.UserText = frm.txt.value ;
}
</script>
</html>
数据挖掘工具
2. 把产生的"test.htm"和"ActiveXDotNet.dll"文件全部都拷贝到机器的虚拟目录下,一般来说虚拟目录是"C:Inetpubwwwroot"。
数据挖掘实验室
3. 打开浏览器,在浏览器的地址栏中输入"http://localhost/test.htm"后,单击"转到"按钮 数据挖掘实验室
至此Visual C#产生的Active X组件和测试这个组件的全部工作就完成了。 数据挖掘实验室
五.总结:
虽然本文介绍的方法的确能够方便的解决Web页面中很多棘手的问题,本文介绍用Visual C#产生的组件的在实用性上的确非常的类似Active X组件,但从本质上说,本文产生的组件并不是真正意义上的Active X组件。如要使用本文所创建的组件,必须在Web页面所在机器上安装.Net框架,客户端访问Web页面时,也不会真正下载本文介绍的组件,从而也不需要设定计算机的安全级别就能够访问使用此组件的Web页面。可见本文产生的组件其实质也是一个托管的代码文件。它只是巧妙的用定义接口的方式来告诉COM/COM+对象,本组件有一个可供访问的公用属性,通过对此属性的读写操作,完成类似Active X组件的工作。
http://www.cnblogs.com/tanghuawei/archive/2006/11/02/547666.html