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)
{
……….
}
以上所述就是本文的全部内容了,希望大家能够喜欢。
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短