在.net應用程式中使用使用者控制項
來源:互聯網
上載者:User
程式|控制項 在.net應用程式中使用使用者控制項
鄭佐2004-11-30
做過asp.net的人都知道開發的時候使用使用者控制項很方便,為功能模組化提供了相當大的靈活性。令人高興的是開發Windows表單也可以使用使用者控制項。這裡我們來看看為使用者控制項添加屬性和事件,並實現把訊息發送到父容器。本文主要是為沒有使用過使用者控制項的朋友提供一些參考。
使用者控制項的實現比較簡單,直接從System.Windows.Forms.UserControl。
public class UserControl1 : System.Windows.Forms.UserControl
為了便於測試我在上面添加了一個TextBox,並註冊TextBox的TextChanged事件,
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
事件處理函數,
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
MessageBox.Show(this.textBox1.Text);
}
這裡示範如果控制項中文字框的內容改變就會用MessageBox顯示當前的文字框內容。
控制項顯示如下:
在表單中添加上面的使用者控制項,當我們改變textBox的文本時,可以看到跳出一個對話方塊,很簡單吧。
下面來看看對控制項添加屬性。
這裡定義一個私人變數。
private string customValue;
添加訪問他的屬性
public string CustomValue
{
get{return customValue;}
set{customValue =value;}
}
在表單中使用的時候像普通控制項一樣進行訪問,
userControl11.CustomValue = "使用者控制項自訂資料";
通過事件可以傳遞訊息到表單上,在定義之前我們先來寫一個簡單的參數類。
public class TextChangeEventArgs : EventArgs
{
private string message;
public TextChangeEventArgs(string message)
{
this.message = message;
}
public string Message
{
get{return message;}
}
}
定義委託為,
public delegate void TextBoxChangedHandle(object sender,TextChangeEventArgs e);
接下去在使用者控制項中添加事件,
//定義事件
public event TextBoxChangedHandle UserControlValueChanged;
為了激發使用者控制項的新增事件,修改了一下代碼,
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
if(UserControlValueChanged != null)
UserControlValueChanged(this,new TextChangeEventArgs(this.textBox1.Text));
}
好了,為了便於在Csdn上回答問題,把完整的代碼貼了出來:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace ZZ.WindowsApplication1
{
public class UserControl1 : System.Windows.Forms.UserControl
{
private System.Windows.Forms.TextBox textBox1;
private string customValue;
private System.ComponentModel.Container components = null;
public string CustomValue
{
get{return customValue;}
set{customValue =value;}
}
//定義事件
public event TextBoxChangedHandle UserControlValueChanged;
public UserControl1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region 組件設計器產生的程式碼
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
this.textBox1.Location = new System.Drawing.Point(12, 36);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
this.Controls.Add(this.textBox1);
this.Name = "UserControl1";
this.Size = new System.Drawing.Size(150, 92);
this.ResumeLayout(false);
}
#endregion
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
if(UserControlValueChanged != null)
UserControlValueChanged(this,new TextChangeEventArgs(this.textBox1.Text));
}
}
//定義委託
public delegate void TextBoxChangedHandle(object sender,TextChangeEventArgs e);
public class TextChangeEventArgs : EventArgs
{
private string message;
public TextChangeEventArgs(string message)
{
this.message = message;
}
public string Message
{
get{return message;}
}
}
}
使用時要在表單中註冊上面的事件,比較簡單都貼原始碼了,
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace ZZ.WindowsApplication1
{
public class Form1 : System.Windows.Forms.Form
{
private WindowsApplication1.UserControl1 userControl11;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
userControl11.CustomValue = "使用者控制項自訂資料";
userControl11.UserControlValueChanged += new TextBoxChangedHandle(userControl11_UserControlValueChanged);
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 表單設計器產生的程式碼
private void InitializeComponent()
{
this.userControl11 = new WindowsApplication1.UserControl1();
this.SuspendLayout();
this.userControl11.Location = new System.Drawing.Point(8, 8);
this.userControl11.Name = "userControl11";
this.userControl11.Size = new System.Drawing.Size(150, 84);
this.userControl11.TabIndex = 0;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 193);
this.Controls.Add(this.userControl11);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void userControl11_UserControlValueChanged(object sender, TextChangeEventArgs e)
{
MessageBox.Show("當前控制項的值為:" + e.Message);
}
}
}
另外需要動態載入,就把控制項添加在容器的Controls集合就行了,下面是在建構函式中添加控制項,
public Form1()
{
InitializeComponent();
UserControl1 uc = new UserControl1();
uc.CustomValue = "動態載入的使用者控制項";
uc.UserControlValueChanged += new TextBoxChangedHandle(userControl11_UserControlValueChanged);
this.Controls.Add(uc);
}
另外從VS.net中的工具箱中拖動使用者控制項到表單上,如果是第一次需要編譯一下項目。