鄭佐
<chsdate w:st="on" isrocdate="False" islunardate="False" day="30" month="11" year="2004"><span lang="EN-US" style="FONT-SIZE: 9pt">2004-11-30</span></chsdate>
做過
asp.net
的人都知道開發(fā)的時(shí)候使用用戶控件很方便,為功能模塊化提供了相當(dāng)大的靈活性。令人高興的是開發(fā)
Windows
窗體也可以使用用戶控件。這里我們來看看為用戶控件添加屬性和事件,并實(shí)現(xiàn)把消息發(fā)送到父容器。本文主要是為沒有使用過用戶控件的朋友提供一些參考。
用戶控件的實(shí)現(xiàn)比較簡單,直接從
System.Windows.Forms.UserControl
。
public
class
UserControl1 : System.Windows.Forms.UserControl
為了便于測試我在上面添加了一個(gè)
TextBox,
并注冊
TextBox
的
TextChanged
事件,
this
.textBox1.TextChanged +=
new
System.EventHandler(
this
.textBox1_TextChanged);
事件處理函數(shù),
private
void
textBox1_TextChanged(
object
sender, System.EventArgs e)
{
MessageBox.Show(
this
.textBox1.Text);
}
這里演示如果控件中文本框的內(nèi)容改變就會(huì)用
MessageBox
顯示當(dāng)前的文本框內(nèi)容。
控件顯示如下:
<shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype>
在窗體中添加上面的用戶控件,當(dāng)我們改變
textBox
的文本時(shí),可以看到跳出一個(gè)對話框,很簡單吧。
下面來看看對控件添加屬性。
這里定義一個(gè)私有變量。
private
string
customValue;
添加訪問他的屬性
public
string
CustomValue
{
get
{
return
customValue;}
set
{customValue =
value
;}
}
在窗體中使用的時(shí)候像普通控件一樣進(jìn)行訪問,
userControl11.CustomValue = "
用戶控件自定義數(shù)據(jù)
";
通過事件可以傳遞消息到窗體上,在定義之前我們先來寫一個(gè)簡單的參數(shù)類。
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;
為了激發(fā)用戶控件的新增事件,修改了一下代碼,
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
組件設(shè)計(jì)器生成的代碼
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;}
}
}
}
使用時(shí)要在窗體中注冊上面的事件,比較簡單都貼源代碼了,
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 = "
用戶控件自定義數(shù)據(jù)
";
userControl11.UserControlValueChanged +=
new
TextBoxChangedHandle(userControl11_UserControlValueChanged);
}
protected
override
void
Dispose(
bool
disposing )
{
if
( disposing )
{
if
(components !=
null
)
{
components.Dispose();
}
}
base
.Dispose( disposing );
}
#region
Windows
窗體設(shè)計(jì)器生成的代碼
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
<place w:st="on">Main</place>()
{
Application.Run(
new
Form1());
}
private
void
userControl11_UserControlValueChanged(
object
sender, TextChangeEventArgs e)
{
MessageBox.Show("
當(dāng)前控件的值為
:" + e.Message);
}
}
}
另外需要?jiǎng)討B(tài)加載,就把控件添加在容器的
Controls
集合就行了,下面是在構(gòu)造函數(shù)中添加控件,
public
Form1()
{
InitializeComponent();
UserControl1 uc =
new
UserControl1();
uc.CustomValue = "
動(dòng)態(tài)加載的用戶控件
";
uc.UserControlValueChanged +=
new
TextBoxChangedHandle(userControl11_UserControlValueChanged);
this
.Controls.Add(uc);
}
另外從
VS.net
中的工具箱中拖動(dòng)用戶控件到窗體上,如果是第一次需要編譯一下項(xiàng)目。
在.net應(yīng)用程序中使用用戶控件