覆盖Button_click事件处理程序

时间:2009-01-18 03:06:03

标签: c# winforms event-handling

我正在使用MVP模式开发WinForms应用程序。我想将按钮点击的标签值传递给演示者。因为我想获得button.Tag属性,所以我需要sender参数为Button类型。如何做到这一点:

private void button0_Click(object sender, EventArgs e)
{
    if (sender is Button)
    {
        presenter.CheckLeadingZero(sender as Button);
    }
}

我不得不将对象向下转换为方法参数中的按钮。

1 个答案:

答案 0 :(得分:3)

如果您要使用is关键字,则使用as关键字检查类型是没有意义的,因为as会执行is检查无论如何,通过明确的演员。相反,你应该做这样的事情:

Button button = sender as Button;
if (button != null)
{
  presenter.CheckLeadingZero(button);
}
相关问题