软件编程
位置:首页>> 软件编程>> C#编程>> C#通过委托调用Button单击事件的方法

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

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

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

这里介绍通过委托取消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)
{
……….
}

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

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com