1. 程式人生 > >c# 共享事件處理程序

c# 共享事件處理程序

使用 stat nbut c# 實例 一個 exceptio end exce

使用同一個方法來處理多個Button實例的Click事件。

1、全選所有的Button,在事件添加中的Click點擊事件中添加處理函數。

技術分享

2、假如一個label控件用於顯示按鈕按下輸出文本

3、處理函數程序

 private void OnButtonClick(object sender, EventArgs e)
        {
            Button btnClicked = (Button)sender;//強制類型轉換object sender轉換成Button
            label1.Text = string.Format("單擊了“{0}”按鈕",btnClicked .Text );
        }

//
// 摘要:
// 將指定字符串中的一個或多個格式項替換為指定對象的字符串表示形式。
//
// 參數:
// format:
// 復合格式字符串。
//
// arg0:
// 要設置格式的對象。
//
// 返回結果:
// format 的副本,其中的任何格式項均替換為 arg0 的字符串表示形式。
//
// 異常:
// T:System.ArgumentNullException:
// format 為 null。


//
// T:System.FormatException:
// format 中的格式項無效。- 或 - 格式項的索引不為零。
public static String Format(String format, object arg0);

c# 共享事件處理程序