从函数调用按钮的OnClick [英] Calling A Button OnClick from a function

查看:80
本文介绍了从函数调用按钮的OnClick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的OnClick = Button_Click一个按钮。我想从另一个函数调用Button_Click但问题是,我需要给它:

I have a button with OnClick=Button_Click. I want to call Button_Click from another function but the problem is that I need to give it:

(object sender, EventArgs e)

我应该为这些参数输入什么?有周围没有任何办法?

What should I enter for those parameters? Is there any way around it?

推荐答案

您可以做到这一点。

 Button_Click(null,EventArgs.Empty);

虽然我同意,这是更好地提取函数,可以从任何地方被调用。

although I agree that it's better to extract function that could be called from anywhere.

例如,如果您有

protected void Button_Click(object sender, EventArgs e)
{
  //some list of code      
}

这code应放在一些新的方法,然后从所谓Button_Click或任何其他方法

this code should be put in some new method and then called from Button_Click or any other method

private void ExtractedMethod()
{ 
 //some list of code
}

 protected void Button_Click(object sender, EventArgs e)
 {
  ExtractedMethod();    
 }

我建议你读一本书 重构:改善既有code的设计 Martin Fowler的的。这是一个架子上一绝。你会不时回来那本书的时候,它是永恒的。

I recommend you to read a book Refactoring: Improving the Design of Existing Code by Martin Fowler. It's a must on a shelf. You will come back to that book from time to time, it's timeless.

这篇关于从函数调用按钮的OnClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆