科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件用Visual C#做WinForm组件

用Visual C#做WinForm组件

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

我们知道.NET的开发平台提了一个名称空间System.Windows.Forms,在此名称空间中提供了许多开发Windows Form的类和对象。

作者:阿虎 来源:yesky 2007年11月14日

关键字:

  • 评论
  • 分享微博
  • 分享邮件
五. 编译组件:

  到目前为止,我们所做的工作和正常的应用程序的内部编写一个类没有什么区别,所不同的是下面的编译过程,我们编译的结果是创建一个库,而不是一个应用程序。具体的编译命令如下:

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#已经在底层把这些问题给处理好了。

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章