科技行者

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

知识库

知识库 安全导航

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

用Visual C#做WinForm组件

  • 扫一扫
    分享文章到微信

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

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

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

关键字:

  • 评论
  • 分享微博
  • 分享邮件
四. 自定义组件的源程序代码( 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 ;
   }
  }
 }
}

  至此,我们已经完成了一个新的组件的构建过程。下面我们将编译源程序文件,生产组件.
    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

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

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