VS2005 winform用户控件(一):入门篇

来源:互联网 发布:怎么删除mac上office 编辑:程序博客网 时间:2024/06/08 06:06

   

VS2005 winform用户控件入门一
本例用于用户控件的初学者参考。
创建一个最简单的用户控件,该控件扩展现有的textbox的功能:
要完成的功能是:当textbox中输入时,用MessageBox显示textbox的内容。
一、创建一个用户控件项目:新建项目/windows项目/windows控件库。即可创建一个用户控件的模板。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsControlLibrary1
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }
}
}
二、在新创建的“UserControl1”设计器上添加一个TextBox.
三、注册TextChanged事件,交添加TextChanged事件的代码:
private void UserControl1_Load(object sender, EventArgs e)
        {
            this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
 
 
        }
        private void textBox1_TextChanged(object sender, System.EventArgs e)
        {
 
            MessageBox.Show(this.textBox1.Text);
 
        }
四、编译你的控件项目,生成一个DLL文件。
五、新建一个Windows项目。
六、在工具栏中“选择项”,然后点击“浏览”,选择你编译的DLL文件,将控件添加到工具栏中。

七、将你新建的控件拖放到窗口中。即可。