Calling code of Button from another one in C#

蓝咒 提交于 2019-12-24 10:10:08

问题


I need to know if it's possible to call the Click of a button from another one.

private void myAction_Click(object sender, EventArgs e)
{
    // int x;
    // ...
}

private void Go_Click(object sender, EventArgs e)
{
    // call the myAction_Click button
}

Thanks.


回答1:


You want:

private void Go_Click(object sender, EventArgs e)
{
    myAction_Click(sender, e);
}

But a better design is to pull the code out:

private void myAction_Click(object sender, EventArgs e)
{
    DoSomething();
}

private void Go_Click(object sender, EventArgs e)
{
    DoSomething();
}

private void DoSomething()
{
    // Your code here
}



回答2:


The button has a PerformClick method.

http://msdn.microsoft.com/en-us/library/hkkb40tf(v=vs.90).aspx



来源:https://stackoverflow.com/questions/5985378/calling-code-of-button-from-another-one-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!