我们往往都是使用C#自己固有的事件,例如点击按钮将自动激发click事件。那么,我们自己如何设计一个事件,并在某种状态下激发该事件呢
解决方案 »
- c# tabcontrol 如何让tabpage背景透明呢。
- 更改属性时实现属性的序列化问题。。。
- 想保存一系列的点的坐标值,用什么方法最好?
- 如何判断系统是否安装了IIS,并且是否支持asp。和IIS版本等信息?
- 跪求——带验证码的外域登陆问题
- vs2010的项目在vs2012下打开,编译通过,但是Visual Studio总提示找不到相关引用
- 紧急问题,高分!!C#我想做个模拟键盘输入中文的问题!!
- wpf datagrid 获取已加载所有数据的方法
- 页面
- 请问在C#中如何防止(或提示)用户输入在文本框中的不能为字母????
- C# 关于hashtable的key碰撞的问题
- kostion进,我说话算术
2.在你的类中委托定义事件对象 public even test objTest
3.在你的类中在你需要的地方发送事件消息 objTest("send"或null,你的参数值);
4.在你需要结果的地方响应事件 你的类.test+=EventHandler(你的方法名称)// 接着就可以在你的方法里得到数据
自定义事件一般用于更好的类的交互分装也用于winform的用户控件
GoodLuck!!!
{
class Program
{
static void Main(string[] args)
{
Yundongyuan y = new Yundongyuan("xiaoli");
Yundongyuan y1 = new Yundongyuan("xiaozhang");// 实例化运动员
Caipanyuan cp = new Caipanyuan();//实例化裁判
cp.del += new delrun(y.Run);
cp.del += new delrun(y1.Run);//订阅事件
cp.Kaiqiang();
Console.ReadLine();
}
}
public delegate void delrun();// 声明委托
public class Yundongyuan
{
private string name;
public Yundongyuan(string name)
{
this.name = name;
}
public void Run()
{
Console.WriteLine("{0}开始跑",this.name);
}
}
public class Caipanyuan
{
public event delrun del;// 创建基于委托的事件
public void Kaiqiang()
{
Console.WriteLine("peng!开始跑");
if (del != null)
del();// 开枪激发事件
}
}
}