• 自定义事件


     1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;
    5
    6 namespace 事件
    7 {
    8 public class ctrEvent
    9 {
    10 public delegate void SomeHandler(object sender,System.EventArgs e);// declare a delegate
    11 public event SomeHandler someEvent;//declare a delegate event
    12 public ctrEvent()
    13 {
    14 //bind event
    15 this.someEvent += new SomeHandler(ctrEvent_someEvent);
    16 }
    17
    18 void ctrEvent_someEvent(object sender, EventArgs e)
    19 {
    20 //throw new NotImplementedException();
    21 Console.WriteLine("雪夜尘封:"+e.ToString());
    22 }
    23 public void RaiseSomeEvent()
    24 {
    25 EventArgs e = new EventArgs();
    26
    27 Console.WriteLine("please input 'xue'");
    28 string str = Console.ReadLine();
    29
    30 if (str.ToLower()=="xue")
    31 {
    32 //在用户输入一个xue的时候出发事件
    33 someEvent(this,e);
    34 }
    35 else
    36 {
    37 Console.WriteLine("input is error");
    38 }
    39 }
    40 }
    41 class Program
    42 {
    43 private ctrEvent myCtrEvent = new ctrEvent();//初始化对象
    44 public Program()
    45 {
    46 myCtrEvent.someEvent += new ctrEvent.SomeHandler(myCtrEvent_someEvent);
    47 myCtrEvent.RaiseSomeEvent();//输入正确口令 就会触发事件
    48 }
    49
    50 void myCtrEvent_someEvent(object sender, EventArgs e)
    51 {
    52 //throw new NotImplementedException();
    53 Console.WriteLine("事件响应,也就是事件发生");
    54
    55 }
    56 static void Main(string[] args)
    57 {
    58 Program pr = new Program();
    59 Console.ReadLine();
    60 }
    61 }
    62 }
  • 相关阅读:
    【Codeforces 349B】Color the Fence
    【Codeforces 459D】Pashmak and Parmida's problem
    【Codeforces 467C】George and Job
    【Codeforces 161D】Distance in Tree
    【Codeforces 522A】Reposts
    【Codeforces 225C】Barcode
    【Codeforces 446A】DZY Loves Sequences
    【Codeforces 429B】Working out
    【Codeforces 478C】Table Decorations
    【Codeforces 478C】Table Decorations
  • 原文地址:https://www.cnblogs.com/mxxblog/p/2426056.html
Copyright © 2020-2023  润新知