简体   繁体   English

覆盖Button_click事件处理程序

[英]Override Button_click event handler

I am developing a WinForms application using the MVP pattern. 我正在使用MVP模式开发WinForms应用程序。 I would like to pass the button clicked tag value to the presenter. 我想将按钮单击的标记值传递给演示者。 Because I want to get the button.Tag property I need the sender argument to be of type Button . 因为我想获取button.Tag属性,所以需要sender参数为Button类型。 How can I do this with out doing this: 我该怎么做却不这样做:

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

I am having to downcast the object to a button in the method parameter. 我必须将对象下放到method参数中的按钮上。

There is no point in checking the type using the is keyword if you're just going to use the as keyword, because as does an is check followed by an explicit cast anyway. 目前正在检查使用的类型没有意义is关键词,如果你只是要使用as关键词,因为as做一个is勾选,然后有明确的转换反正。 Instead, you should do something like this: 相反,您应该执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM