C# 实现事件(Event)演习代码
2007-05-31 21:22
661 查看
using System;
using System.Collections.Generic;
using System.Text;
namespace Event
{
class Program
{
//定义包含事件数据的类:
public class myEventArgs : EventArgs //***
{
private bool yes_or_no;
private string txt;
public myEventArgs()
{
}
public myEventArgs(bool yn, string s)
{
YorN = yn;
txt = s;
}
public bool YorN
{
get
{
return yes_or_no;
}
set
{
yes_or_no = value;
}
}
public string Txt
{
get
{
return Txt;
}
set
{
txt = value;
}
}
public string ReturnTxt()
{
if (yes_or_no)
{
return "Hello: " + txt + " YES! YES! YES!";
}
else
{
return "Hello: " + txt + " Not NO!";
}
}
}
/// <summary>
/// 声名委托
/// </summary>
/// <param name="sender">表示事件的引发者</param>
/// <param name="e">事件参数</param>
public delegate void myEventHandler(Object sender, myEventArgs e); //***
//
public class App
{
private bool yn;
private string txt;
public bool YorN
{
get { return yn;}
set { yn = value; }
}
public string Text
{
get { return txt; }
set { txt = value; }
}
public event myEventHandler myEvHdl; //***
protected virtual void OnFire(myEventArgs e)
{
if (myEvHdl != null)
{
myEvHdl(this, e); //调用委托
}
}
public void Run()
{
myEventArgs e = new myEventArgs(yn, txt);
OnFire(e);
}
}
public class Winform //模拟 Windows 的 Form
{
public void DemoOnMouseClick(object sender, myEventArgs e) //模拟鼠标点击事件
{
Console.WriteLine(e.ReturnTxt());
}
}
/// <summary>
/// 主类:程序的主流程
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
Winform winform = new Winform(); //事件接收者
App app = new App(); //事件源
//绑定DemoOnMouseClick方法到事件myEvHdl
app.myEvHdl += new myEventHandler(winform.DemoOnMouseClick); //***
app.YorN = true;
app.Text = "网眼的 http://www.why100000.com!";
app.Run();
app.YorN = false;
app.Text = "网眼的 http://why100000.com!";
app.Run();
Console.ReadLine();
}
}
}
电脑学习网:http://www.why100000.com
张庆 2007.5.31
using System.Collections.Generic;
using System.Text;
namespace Event
{
class Program
{
//定义包含事件数据的类:
public class myEventArgs : EventArgs //***
{
private bool yes_or_no;
private string txt;
public myEventArgs()
{
}
public myEventArgs(bool yn, string s)
{
YorN = yn;
txt = s;
}
public bool YorN
{
get
{
return yes_or_no;
}
set
{
yes_or_no = value;
}
}
public string Txt
{
get
{
return Txt;
}
set
{
txt = value;
}
}
public string ReturnTxt()
{
if (yes_or_no)
{
return "Hello: " + txt + " YES! YES! YES!";
}
else
{
return "Hello: " + txt + " Not NO!";
}
}
}
/// <summary>
/// 声名委托
/// </summary>
/// <param name="sender">表示事件的引发者</param>
/// <param name="e">事件参数</param>
public delegate void myEventHandler(Object sender, myEventArgs e); //***
//
public class App
{
private bool yn;
private string txt;
public bool YorN
{
get { return yn;}
set { yn = value; }
}
public string Text
{
get { return txt; }
set { txt = value; }
}
public event myEventHandler myEvHdl; //***
protected virtual void OnFire(myEventArgs e)
{
if (myEvHdl != null)
{
myEvHdl(this, e); //调用委托
}
}
public void Run()
{
myEventArgs e = new myEventArgs(yn, txt);
OnFire(e);
}
}
public class Winform //模拟 Windows 的 Form
{
public void DemoOnMouseClick(object sender, myEventArgs e) //模拟鼠标点击事件
{
Console.WriteLine(e.ReturnTxt());
}
}
/// <summary>
/// 主类:程序的主流程
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
Winform winform = new Winform(); //事件接收者
App app = new App(); //事件源
//绑定DemoOnMouseClick方法到事件myEvHdl
app.myEvHdl += new myEventHandler(winform.DemoOnMouseClick); //***
app.YorN = true;
app.Text = "网眼的 http://www.why100000.com!";
app.Run();
app.YorN = false;
app.Text = "网眼的 http://why100000.com!";
app.Run();
Console.ReadLine();
}
}
}
电脑学习网:http://www.why100000.com
张庆 2007.5.31
相关文章推荐
- .Net/C# 封装磁盘目录文件搜索功能的工具类 (实现了与搜索相关的事件,以便插入客户处理代码)
- 4000 C#之弱事件(Weak Event)的实现
- 编写高质量代码改善C#程序的157个建议——建议41:实现标准的事件模型
- Redis总结(五)缓存雪崩和缓存穿透等问题 Web API系列(三)统一异常处理 C#总结(一)AutoResetEvent的使用介绍(用AutoResetEvent实现同步) C#总结(二)事件Event 介绍总结 C#总结(三)DataGridView增加全选列 Web API系列(二)接口安全和参数校验 RabbitMQ学习系列(六): RabbitMQ 高可用集群
- C#代码:用事件模式实现通知
- c#控件实现类似c++中ocx控件功能,Event事件
- C# 的委托事件实现(含代码)
- .Net/C# 封装磁盘目录文件搜索功能的工具类 (实现了与搜索相关的事件,以便插入客户处理代码)
- .Net/C# 封装磁盘目录文件搜索功能的工具类 (实现了与搜索相关的事件,以便插入客户处理代码)
- .Net/C# 封装磁盘目录文件搜索功能的工具类 (实现了与搜索相关的事件,以便插入客户处理代码)
- 用 C++ 实现 C# 中的 委托/事件 (2-delegate event functor)
- 改进C#代码之25:用事件模式实现通知
- C#批量重命名文件代码的实现
- C#事件(event)解析(转)
- c# 调用API mouse_event 模拟鼠标事件
- C#中运用事件实现异步调用
- MD5原理及c#代码实现
- C#窗体的最大化/最小化/还原/最小化到托盘/NotifyIcon的代码实现
- C#:代表(delegate)和事件(event)
- C#实现汉字转换为拼音缩写的代码