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