• C# 事件的简单例子


    事件的简单例子
    using System.Windows,Form;
    public class Form1:Form
    {
        public delegate void ActionEventHandler(object sender,ActionCancelEventArgs ev);//定义委托类型ActionEventHandler
        public static event ActionEventHandler Action;//定义事件
        
        BusEntity busEntity=new BusEntity();
        
        public Form()
        {
            InitializeComponent();
        }
        
        /*public void Form1_Load()
        {
            ActionCancelEventArgs e = new ActionCancelEventArgs();
            Action+=new ActionEventHandler(OnAction);//触发事件
        }*/
        
        public static void OnAction(object sender,ActionCancelEventArgs ev)
        {
            if(Action!=null)
            {
                Action(sender,ev);
            }
        }
        
        private void btnRaise_Click(object sender,EventAgrs e)
        {
            ActionCancelEventArgs cancelEvent=new ActionCancelEventArgs();
            OnAction(this,cancelEvent);
            if(cancelEvent.Cancel)
            {
                lblInfo.Text=cancelEvent.Message;
            }
            else
            {
                lblInfo.Text=busEntity.TimeString;
            }
        }
    }
    
    public class ActionCancelEventArgs:System.ComponentModel.CancelEventArgs
    {
        public string Message
        {get;set;}
        public ActionCancelEventArgs():this(false){}//构造函数
        
        public ActionCancelEventArgs(bool cancel):this(false,""){}//构造函数
        
        public ActionCancelEventArgs(bool cancel,string message):base(cancel)//构造函数
        {
            this.message=message;
        }
    }
    /*
    基于EventArgs的新类ActionCancelEventArgs派生自CancelEventArgs,CancelEventArgs
    派生自EventArgs。CancelEventArgs添加了Cancel属性,该属性是一布尔值,它通知Senser
    对象,接收器希望取消或停止事件的处理。在ActionCancelEventArgs里还添加了
    Message属性。
    */
    
    using System;
    namespace test
    {
        public class BusEntity
        {
            string time="";
            
            public BusEntity()
            {
                Form1.Action += new Form1.ActionEventHandler(Form1_Action); 
            }
            
            private void Form1_Action(object sender,ActionCancelEventArgs e)
            {
                e.Cancel=! DoActions();
                if(e.Cancel)
                {
                    e.Message="wasn't the right time";
                }
            }
            
            private bool DoActions()
            {
                bool retVal=false;
                DateTime tm=DateTime.Now;
                
                if(tm.Second<30)
                {
                    time="the tine is"+DateTime.Now.ToLongTimeString();
                    retVal=true;
                }
                else
                {
                    time="":
                    return retVal;
                }
            }
            
            public string TimeString
            {
                get
                {
                    return time;
                }
            }
        }
    }
  • 相关阅读:
    hibernate中持久化对象的生命周期(转载)
    IDEA调试技巧之条件断点
    POI中不推荐的方法与其替代的方法
    visualvm监控类是否是多例模式
    IDEA中Maven项目使用Junit4单元测试的写法
    JPQL的关联查询
    poi的cellstyle陷阱,样式覆盖
    studio 连不上远程仓库的各种原因分析
    Concurrent usage detected
    我的SSH框架实例(附源码)
  • 原文地址:https://www.cnblogs.com/refactor/p/2682364.html
Copyright © 2020-2023  润新知