static
void
Main(
string
[] args)
{
MyText myText =
new
MyText();
myText.Changed +=
new
MyText.ChangedEventHandler(myText_Chenged);
string
str =
string
.Empty;
while
(str !=
"exit"
)
{
Console.Write(
"请输入一个字符串:"
);
str = Console.ReadLine();
myText.Text = str;
}
}
private
static
void
myText_Chenged(MyText sender)
{
Console.WriteLine(
"Text属性的值改变:{0}"
, sender.Text);
}
public
class
MyText
{
//定义事件委托
public
delegate
void
ChangedEventHandler(MyText sender);
//定义一个委托类型事件
public
event
ChangedEventHandler Changed;
//用于触发Changed事件
protected
virtual
void
OnChanged()
{
if
(
this
.Changed !=
null
)
{
this
.Changed(
this
);
}
}
private
string
_text =
string
.Empty;
public
string
Text
{
get
{
return
this
._text; }
set
{
this
._text = value;
this
.OnChanged();
}
}
}
这个例子没看懂,不知道为什么当有textBOX事件时,就会触发。
textBox在文本改变的时候会触发textchanged事件
myText.Changed += new MyText.ChangedEventHandler(myText_Chenged);
这一行挂钩到myText_Chenged