C#通过委托调用Button单击事件的方法

作者:hebedich 时间:2022-11-29 22:09:15 

这里介绍通过委托取消Button事件switch-case的方法。需要注意的是,事先要按顺序在各个Button的Tag属性中设置0、1、2、3……等序号,其作用请详看代码。


/*定义委托*/
public delegate 类型或viod MethodDelegate(参数1, 参数2);

private void buttonC_Click(object sender, EventArgs e)
{
Button button = (Button)sender;

/*向委托添加方法*/
   MethodDelegate methodDelegate = 你的方法1;
   methodDelegate +=你的方法2;
   methodDelegate +=你的方法3;
     ……….
   /*转换成数组*/
   Delegate[] delegates = methodDelegate.GetInvocationList();

/*根据button.Tag中序号选择委托列表数组中相应方法*/
   MethodDelegate method = (MethodDelegate)delegates[Convert.ToInt16(button.Tag)];

/*执行*/
   类型 i = method(参数1, 参数2);
}

Private 类型或viod 你的方法1 (参数1, 参数2)
{
……….
}

Private 类型或viod 你的方法2 (参数1, 参数2)
{
……….
}
Private 类型或viod 你的方法3 (参数1, 参数2)
{
……….
}

以上所述就是本文的全部内容了,希望大家能够喜欢。

标签:C#,委托调用,Button单击事件
0
投稿

猜你喜欢

  • SpringBoot拦截器Filter的使用方法详解

    2023-06-09 07:33:59
  • C语言数据的存储专项分析

    2022-05-05 13:35:24
  • LINQ操作符SelectMany的用法

    2021-08-22 18:20:37
  • C语言中fchdir()函数和rewinddir()函数的使用详解

    2022-02-08 20:08:08
  • SpringData JPA中@OneToMany和@ManyToOne的用法详解

    2021-10-01 00:49:10
  • 教你怎么使用Java实现WebSocket

    2022-10-31 04:08:50
  • Android高级组件ImageSwitcher图像切换器使用方法详解

    2023-11-07 13:18:19
  • C#仿Windows XP自带的扫雷游戏

    2023-07-30 07:40:48
  • init output stream初始化输出流源码分析

    2023-01-08 09:53:20
  • 一步步教你如何创建第一个C#项目

    2021-08-15 04:41:56
  • Android 中使用EditText 点击全选再次点击取消全选功能

    2023-09-08 00:08:44
  • 详解SpringBoot配置文件启动时动态配置参数方法

    2022-03-05 03:20:02
  • C# 列表List的常用属性和方法介绍

    2022-09-04 07:52:05
  • SpringMVC接收复杂集合对象(参数)代码示例

    2023-01-29 18:33:51
  • eclipse连接不到genymotion问题的解决方案

    2022-09-05 23:26:16
  • Java中常用的6种排序算法详细分解

    2022-01-08 20:19:18
  • 基于spring security实现登录注销功能过程解析

    2023-11-29 06:09:05
  • Java中的Map集合简单汇总解析

    2023-08-22 19:12:07
  • Java API文档的使用方法详解

    2022-06-15 20:12:03
  • 一文详解Spring security框架的使用

    2023-06-03 22:42:09
  • asp之家 软件编程 m.aspxhome.com