這里介紹通過委托取消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){……….}
以上所述就是本文的全部內容了,希望大家能夠喜歡。
新聞熱點
疑難解答