用Visual C#做WinForm组件
信息来源:邪恶八进制信息安全团队(
www.eviloctal.com)
我们知道.NET的开发平台提了一个名称空间System.Windows.Forms,在此名称空间中提供了许多开发Windows Form的类和对象,这些类和对象不仅内容十分丰富,而且还具有十分强大的拓展性,可以利用这些类和对象,简单而迅速的开发出自己需要的组件来。本文就将介绍如何利用.Net 开发平台原有的类和对象写一个自己的WinForm组件,如何编译自己的组件,且如何在一个客户程序中使用它。
一. 程序设计和运行的基本环境:
(1).视窗2000服务器版
(2)..Net FrameWork SDK Beta 2 版
二. 本文中开发的组件功能介绍:
(1).本文中开发的组件是一个自定义的组件,它是由二个组件合并而成的,一个是标签组件( Lable ),另外一个是文本框组件( TextBox )。
(2).自定义组件中定义了了二个新的属性,一个属性是Text,这个属性是通过派生原有的文本框中的Text属性而得到的;另外一个属性是LabelText,它是通过继承了原有的标签的Text属性而得到的。
(3).组件的用途。
在程序设计中,很多时候是要定义一个标签,标签显示要输入的文本内容。然后再定义一个文本框,填入信息。使用了本组件后,只要定义一个此组件,然后对组件属性设定不同的值就可以了。这样就简化了程序设计过程。这一点将在后面的组件应用中得到体现。
三. 开发组件中的难点和重点:
(1).如何设定自定义组件的内容:
本组件是由标签组件和文本框组件组成的,首先要定义此组件的组成结构。具体的程序设计如下:
//LabledTextBox组件是继承了 UserControl组件的
public class LabeledTextBox : UserControl
{
//定义本组件的组成结构
private Label myLabel ;
private TextBox myTextBox ;
……
}
(2).如何派生文本框中的Text属性,而生成自己的新的属性:
由于是派生文本框的Text属性,所以在程序中用到了关键字"override"。并且通过关键字"set"设定属性,"get"关键字来读取组件的属性值。具体程序设计如下:
//组件中的Text属性,是从文本框的Text的属性派生而来
public override string Text
{
get
{
return myTextBox.Text ;
}
set
{
myTextBox.Text = value ;
}
}
(3).如何创建一个新的属性LabelText,并且此属性值是通过继承现有的标签的"Text"属性而得到的。具体的程序设计如下:
//创建一个新的属性LabelText,并且此属性的值是通过继承此组件中的标签的Text属性值
public string LabelText
{
get
{
return myLabel.Text ;
}
set
{
myLabel.Text = value ;
}
}
四. 自定义组件的源程序代码( control.cs ):
control.cs源代码如下:
using System.Windows.Forms ;
//定义封装此组件的名称空间
namespace MyControls
{
//LabledTextBox组件是继承了 UserControl组件的
public class LabeledTextBox : UserControl
{
//定义本组件的组成结构
private Label myLabel ;
private TextBox myTextBox ;
public LabeledTextBox ( )
{
InitializeComponent ( ) ;
}
public void InitializeComponent ( )
{
//定义一个标签
myLabel = new Label ( ) ;
myLabel.Location = new System.Drawing.Point ( 0 , 0 ) ;
myLabel.Size = new System.Drawing.Size ( 100 , 20 ) ;
//定义一个文本框
myTextBox = new TextBox ( ) ;
myTextBox.Location = new System.Drawing.Point ( 105 , 0 ) ;
myTextBox.Size =new System.Drawing.Size ( 100 , 20 ) ;
//同样要设定所希望的组件大小
this.Size =new System.Drawing.Size ( 205 , 20 ) ;
//加入组件
this.Controls.Add ( myLabel ) ;
this.Controls.Add ( myTextBox ) ;
}
//组件中的Text属性,是从文本框的Text的属性派生而来
public override string Text
{
get
{
return myTextBox.Text ;
}
set
{
myTextBox.Text = value ;
}
}
//创建一个新的属性LabelText,并且此属性的值是通过继承此组件中的标签的 Text属性值
public string LabelText
{
get
{
return myLabel.Text ;
}
set
{
myLabel.Text = value ;
}
}
}
}
至此,我们已经完成了一个新的组件的构建过程。下面我们将编译源程序文件,生产组件.
五. 编译组件:
到目前为止,我们所做的工作和正常的应用程序的内部编写一个类没有什么区别,所不同的是下面的编译过程,我们编译的结果是创建一个库,而不是一个应用程序。具体的编译命令如下:
csc /r:system.windows.forms.dll /t:library control.cs
编译完成后,将得到组件control.dll
六. 创建一个简单的客户应用程序:
使用自定义的组件和使用.Net FrameWork SDK中提供的组件没有任何区别,都是按照以下步骤来进行的:
(1).导入组件的名称空间,在应用程序中,就是导入MyControls。具体如下:
using MyControls ;
(2).在程序中定义由此名称空间封装的组件:在程序中,使用了三个新的组件。具体如下:
protected LabeledTextBox name , address , zip ;
(3).设定这些组件的属性,在程序中可以看到是如何设定组件的二个自定义的属性的。下面语句就是介绍如何在程序中定义组件的新属性。
name = new LabeledTextBox ( ) ;
name.Location = new System.Drawing.Point ( 5 , 5 ) ;
name.LabelText = "姓名:" ;
可见和定义其他属性没有什么区别。
(4).把组件加入到窗体中。
下面就是按照以上步骤所得到的源程序代码( sample.cs )和此代码生成的执行文件的运行界面:
sample.cs源程序如下:
using System.Windows.Forms ;
using MyControls ;//导入组件的名称空间
using System ;
public class Form1 : Form
{
//定义新构建的组件
protected LabeledTextBox name , address , zip ;
protected Button show ;
public Form1 ( )
{
InitializeComponent ( ) ;
}
public static void Main ( )
{
Application.Run ( new Form1 ( ) ) ;
}
public void InitializeComponent ( )
{
//创建新的组件,此组件中就封装了标签和文本框
name = new LabeledTextBox ( ) ;
address= new LabeledTextBox ( ) ;
zip = new LabeledTextBox ( ) ;
show= new Button ( ) ;
//设定新组件的属性值,可以看看如何设定Text属性和LabelText属性
name.Location = new System.Drawing.Point ( 5 , 5 ) ;
name.LabelText = "姓名:" ;
address.Location = new System.Drawing.Point ( 5 , 35 ) ;
address.LabelText = "住址:" ;
zip.Location = new System.Drawing.Point ( 5 , 70 ) ;
zip.LabelText = "邮编:" ;
show.Location = new System.Drawing.Point ( 5 , 100 ) ;
show.Text = "显示组件属性值" ;
show.Size = new System.Drawing.Size (100, 25) ;
show.Click += new System.EventHandler ( show_Click ) ;
this.Text = "显示自建组件的LabelText属性和Text属性值!" ;
this.Controls.Add ( name ) ;
this.Controls.Add ( address ) ;
this.Controls.Add ( zip ) ;
this.Controls.Add ( show ) ;
}
protected void show_Click ( object sender , EventArgs e )
{
string message = name.LabelText + " " + name.Text ;
message+="\n" + address.LabelText + " " + address.Text ;
message+="\n" + zip.LabelText + " " + zip.Text ;
MessageBox.Show ( message ,"组件的LabelText属性和Text属性值如下:") ;
}
}
经过一下编译命令:
csc /r:control.dll sample.cs
七. 总结:
面向组件编程是以后程序设计的一个重要的方向,通过以上介绍,我们可以了解如何用Visual C#进行简单的组件编程。在用Visual C#编写组件程序的过程中,也可以看出,比起其他语言来说,用Visual C#编写组件在编写和分发的时候,程序员相对轻松了许多,不需要考虑很多问题了,而这些问题在用其他程序设计语言的时候却是一定要考虑的。为什么?因为Visual C#已经在底层把这些问题给处理好了。