• C#事件快捷设置


    注解:本文摘自网络

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

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

      5、事件触发

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


    实现:

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

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

     1  //打印对象
     2         public class CustomPrint
     3         {
     4             /// <summary>
     5             /// 1、定义事件参数
     6             /// </summary>
     7             public class CustomPrintArgument : EventArgs
     8             {
     9                 private int copies;
    10                 public CustomPrintArgument(int numberOfCopies)
    11                 {
    12                     this.copies = numberOfCopies;
    13                 }
    14                 public int Copies
    15                 {
    16                     get { return this.copies; }
    17                 }
    18             }
    19 
    20             /// <summary>
    21             /// 2、声明事件的委托
    22             /// </summary>
    23             /// <param name="sender"></param>
    24             /// <param name="e"></param>
    25             public delegate void CustomPrintHandler(object sender, CustomPrintArgument e);
    26 
    27             /// <summary>
    28             /// 3、声明事件
    29             /// </summary>
    30             public event CustomPrintHandler CustomPrintEvent;
    31 
    32             /// <summary>
    33             /// 4、定义触发事件
    34             /// </summary>
    35             /// <param name="copyies">份数</param>
    36             public void RaisePrint(int copyies)
    37             {
    38                 CustomPrintArgument e = new CustomPrintArgument(copyies);
    39                 CustomPrintEvent(this, e);
    40             }
    41         }
    42 
    43 
    44 
    45 
    46 
    47 
    48         /// <summary>
    49         /// WinForm 构造函数
    50         /// </summary>
    51         public Form1()
    52         {
    53             InitializeComponent();
    54 
    55             PrintCustom();
    56         }
    57 
    58         /// <summary>
    59         /// 打印方法
    60         /// </summary>
    61         private void PrintCustom()
    62         {
    63             //实例对象
    64             CustomPrint cp = new CustomPrint();
    65 
    66             //添加事件
    67             cp.CustomPrintEvent += new CustomPrint.CustomPrintHandler(cp_CustomPrintEvent);
    68 
    69             //5、触发事件
    70             cp.RaisePrint(10);
    71 
    72         }
    73 
    74         //6、事件处理
    75         void cp_CustomPrintEvent(object sender, CustomPrint.CustomPrintArgument e)
    76         {
    77             int copies = e.Copies;
    78             MessageBox.Show(copies.ToString());
    79         }
    80     }
    事件设定

    作为快捷指南,可以根据这个来做。

  • 相关阅读:
    超300程序员受益入职的面试经验
    【修真院“正直”系列之三】【修真神界】【修行卷】【第一章】修真院入场券
    Js的同步异步以及回调函数
    Bootstrap栅格布局使用
    为什么程序员的工资比其他行业高这么多?
    Java培训机构可靠吗?
    get 与 post的区别?application json 与form表单的区别?
    谈谈以前端角度出发做好SEO需要考虑什么?
    【修真院“纯洁”系列之十九】驱逐令
    Redis缓存穿透和缓存雪崩以及解决方案
  • 原文地址:https://www.cnblogs.com/ningheshutong/p/5206055.html
Copyright © 2020-2023  润新知