2、事件处理程序的 sender 参数分配给变量,将它强制转换为适当的类型。
下面的示例演示由几个不同按钮调用的 Button 控件 click 事件的处理程序。该处理程序显示了与单击按钮有关的信息。
private void Button_Click(object sender, System.EventArgs e)
{
Button b;
b = (Button)sender;
switch (b.ID)
{
case "Button1":
Label1.Text = "You clicked the first button";
break;
case "Button2":
Label1.Text = "You clicked the second button";
break;
case "Button3":
Label1.Text = "You clicked the third button";
break;
}
}