VS2005 winform用户控件入门一
本例用于用户控件的初学者参考。
建立一个最简单的用户控件,该控件扩展示有的textbox的功能:
要完成的功能是:当textbox中输入时,用MessageBox显示textbox的内容。
1、建立一个用户控件项目:新建项目/windows项目/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();
}
}
}
2、在新建立的“UserControl1”设计器上添加一个TextBox.
3、注册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);
}
4、编译你的控件项目,生成一个DLL文件。
5、新建一个Windows项目。
6、在工具栏中“选择项”,而后点击“浏览”,选择你编译的DLL文件,将控件添加到工具栏中。ide
7、将你新建的控件拖放到窗口中。便可。工具
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xjzdr/archive/2008/02/05/2084098.aspxthis