CSDN博客

img atomorrow

简单事件写法

发表于2004/11/2 12:37:00  721人阅读

public delegate void UserRequest(object sender,EventArgs e); //定义委托 
public static event UserRequest OnUserRequest; //此委托类型类型的事件

一个可以加载委托的类
Client( UserRequest ur )
   {
    OnUserRequest += ur;
   }

一个和委托相同输入参数的触发事件后的方法
private static void ShowMessage4(object sender,EventArgs e)
   {
    Console.WriteLine("HaHa3443!!");
   }

运行程序中在需要的地方加载就可
public static void Main()
   {  
 
            new Client( new UserInputMonitor.UserRequest(ShowMessage4));   //加载
            bool tag = true;
            int i=0;
            while( tag )
            {
                 Console.WriteLine(i);
                 i++;
                 if ( i == 50 )
                 {
                      OnUserRequest(new EventArgs(), new EventArgs());           //触发事件
                      Console.Write("55");
                      tag = false;
                 }            
                Console.Read();
              }
}

阅读全文
0 0

相关文章推荐

img
取 消
img