| |

VerySource

 Forgot password?
 Register
Search
View: 826|Reply: 1

C# 自定义事件并使用自定义事件参数方法

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2022-7-7 09:54:38
| Show all posts |Read mode
C# 自定义带自定义参数的事件 需要经过以下几个步骤:
1、自定义事件参数 :要实现自定义参数的事件,首先要自定义事件参数。该参数是个类。继承自EventArgs。
2、声明委托用于事件
3、声明事件
4、定义事件触发 :事件定义后,要有个触发事件的动作。
以上基本上完成了自定义事件。不过还缺事件调用,请看下边两个步骤。
5、事件触发
6、自己编写事件的处理 :事件触发后。要处理事件。
实现:
假设有个打印对象,需要给它自定义一个打印事件。事件参数中传入打印的份数。在程序加载时,调用打印对象,并通过自定义打印参数,实现打印,打印类打完成后传回实际打印的数量(有可能因故障实际打印数量比传入参数少 或要传回其他信息)。此代码是在WinForm下编写。
  1. //打印对象CustomPrint.cs文件
  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.    /// <summary>
  20.    /// 2、声明事件的委托
  21.    /// </summary>
  22.    /// <param name="sender"></param>
  23.    /// <param name="e"></param>
  24.    public delegate void CustomPrintHandler(object sender, CustomPrintArgument e);

  25.    /// <summary>
  26.    /// 3、声明事件
  27.    /// </summary>
  28.    public event CustomPrintHandler CustomPrintEvent;

  29.    /// <summary>
  30.    /// 4、定义触发事件
  31.    /// </summary>
  32.    /// <param name="copyies">份数</param>
  33.    public void RaisePrint(int copyies)
  34.    {
  35.        //int actualCopies = 打印处理函数(copyies);
  36.        CustomPrintArgument e = new CustomPrintArgument(actualCopies);
  37.        CustomPrintEvent(this, e);
  38.    }
  39. }
Copy the Code
  1. //主窗体Form1.cs文件
  2. public class Form1 : Form
  3. {   
  4.     /// <summary>
  5.     /// WinForm 构造函数
  6.     /// </summary>
  7.     public Form1()
  8.     {
  9.          InitializeComponent();
  10.          PrintCustom();
  11.     }

  12.     /// <summary>
  13.     /// 打印方法
  14.     /// </summary>
  15.     private void PrintCustom()
  16.     {
  17.         //实例对象
  18.         CustomPrint cp = new CustomPrint();
  19.         //添加事件
  20.         cp.CustomPrintEvent += new CustomPrint.CustomPrintHandler(cp_CustomPrintEvent);
  21.         //5、触发事件
  22.         cp.RaisePrint(10);
  23.     }

  24.      //6、事件处理
  25.      void cp_CustomPrintEvent(object sender, CustomPrint.CustomPrintArgument e)
  26.      {
  27.          int actualCopies = e.Copies;
  28.          MessageBox.Show(actualCopies.ToString());
  29.      }
  30. }
Copy the Code



Reply

Use magic Report

0

Threads

5

Posts

66.00

Credits

Member

Rank: 2

Credits
66.00

 China

Post time: 2024-8-14 10:44:06
| Show all posts
楼主威武,已阅学习
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list