zoukankan      html  css  js  c++  java
  • C# 自定义事件并使用自定义事件参数方法

    C# 自定义带自定义参数的事件方法


    C# 自定义带自定义参数的事件 需要经过以下几个步骤:
      1、自定义事件参数   :要实现自定义参数的事件,首先要自定义事件参数。该参数是个类。继承自EventArgs
      2、声明委托用于事件  
      3、声明事件
      4、定义事件触发    :事件定义后,要有个触发事件的动作。
      以上基本上完成了自定义事件。不过还缺事件调用,请看下边两个步骤。

      5、事件触发

      6、自己编写事件的处理 :事件触发后。要处理事件。 

    实现:

      假设有个打印对象,需要给它自定义一个打印事件。事件参数中传入打印的份数。在程序加载时,调用打印对象,并通过自定义打印参数,实现打印。

      代码如下,此代码是在WinForm下编写。

            //打印对象
            public class CustomPrint
            {
                
    /// <summary>
                
    /// 1、定义事件参数
                
    /// </summary>
                public class CustomPrintArgument : EventArgs
                {
                    
    private int copies;
                    
    public CustomPrintArgument(int numberOfCopies)
                    {
                        
    this.copies = numberOfCopies;
                    }
                    
    public int Copies
                    {
                        
    get { return this.copies; }
                    }
                }

                
    /// <summary>
                
    /// 2、声明事件的委托
                
    /// </summary>
                
    /// <param name="sender"></param>
                
    /// <param name="e"></param>
                public delegate void CustomPrintHandler(object sender, CustomPrintArgument e);

                
    /// <summary>
                
    /// 3、声明事件
                
    /// </summary>
                public event CustomPrintHandler CustomPrintEvent;

                
    /// <summary>
                
    /// 4、定义触发事件
                
    /// </summary>
                
    /// <param name="copyies">份数</param>
                public void RaisePrint(int copyies)
                {
                    CustomPrintArgument e = new CustomPrintArgument(copyies);
                    CustomPrintEvent(this, e);
                }
            }






            
    /// <summary>
            
    /// WinForm 构造函数
            
    /// </summary>
            public Form1()
            {
                InitializeComponent();

                PrintCustom();
            }

            
    /// <summary>
            
    /// 打印方法
            
    /// </summary>
            private void PrintCustom()
            {
                
    //实例对象
                CustomPrint cp = new CustomPrint();

                
    //添加事件
                cp.CustomPrintEvent += new CustomPrint.CustomPrintHandler(cp_CustomPrintEvent);

                
    //5、触发事件
                cp.RaisePrint(10);

            }

            
    //6、事件处理
            void cp_CustomPrintEvent(object sender, CustomPrint.CustomPrintArgument e)
            {
                
    int copies = e.Copies;
                MessageBox.Show(copies.ToString());
            }
        }


      运行程序,在弹出窗口中会显示10。

  • 相关阅读:
    C# 操作配置文件
    C# Excel操作类
    没有找到 mspdb100.dll 的解决办法
    工厂方法模式
    .Net互操作2
    The certificate used to sign “AppName” has either expired or has been revoked. An updated certificate is required to sign and install the application解决
    手机抓包xcode自带命令行工具配合wireshark实现
    expecting SSH2_MSG_KEX_ECDH_REPLY ssh_dispatch_run_fatal问题解决
    使用ssh-keygen设置ssh无密码登录
    远程复制文件到服务器
  • 原文地址:https://www.cnblogs.com/scottckt/p/1838598.html
Copyright © 2011-2022 走看看