C#通过委托调用Button单击事件的方法
这里介绍通过委托取消Button事件switch-case的方法。需要注意的是,事先要按顺序在各个Button的Tag属性中设置0、1、2、3……等序号,其作用请详看代码。
/*定义委托*/
publicdelegate类型或viodMethodDelegate(参数1,参数2);
privatevoidbuttonC_Click(objectsender,EventArgse)
{
Buttonbutton=(Button)sender;
/*向委托添加方法*/
MethodDelegatemethodDelegate=你的方法1;
methodDelegate+=你的方法2;
methodDelegate+=你的方法3;
……….
/*转换成数组*/
Delegate[]delegates=methodDelegate.GetInvocationList();
/*根据button.Tag中序号选择委托列表数组中相应方法*/
MethodDelegatemethod=(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)
{
……….
}
以上所述就是本文的全部内容了,希望大家能够喜欢。