简体   繁体   English

如何在隐藏按钮上执行 Click-Event

[英]How to perform Click-Event on a hidden button

In my C# form I have two buttons在我的 C# 表单中,我有两个按钮

button1.Hide()
private void button2_Click(object sender, EventArgs e)
{
 button1.PerformClick();
}

The button1 is hidden at form loading, I want the logic behind button1 to be perfomed when it's hidden too. button1 在表单加载时被隐藏,我希望 button1 背后的逻辑也能在隐藏时执行。

Just let the function outside become another function, then you can call function although you hidden the button1.只要让外面的function变成另一个function,然后你可以调用function虽然你隐藏了button1。

private void button1(object sender, EventArgs e)
{
  _button1();
}
private void button2(object sender, EventArgs e)
{
  _button1();
}

//Here is the function
void _button1()
{
    ...
}

If your Button is hidden, it seems that you need the functionality behind not or just in special cases.如果您的 Button 是隐藏的,那么您似乎不需要或仅在特殊情况下需要背后的功能。 Keeping functionality out of events is often a simple solution to avoid problems in the future.将功能排除在事件之外通常是避免将来出现问题的简单解决方案。

    private void btn_Reload_Click(object sender, EventArgs e)
    { 
      // reload here - maybe you reload all your employees from a datasource
    }

    private void btn_Reload_With_Calculation_Click(object sender, EventArgs e)
    {
      // you can use functionality here from a another button and call the 
      btn_Reload_Click(this, EventArgs.Empty); // DON'T DO THIS IN MY OPINION
      // ....
    }

Maybe this solution is better even if you need the functionality at other workflows.即使您需要其他工作流程中的功能,这个解决方案可能会更好。

    private void btn_Reload_Click(object sender, EventArgs e)
    {
      Reload();
    }

    private void btn_Reload_With_Calculation_Click(object sender, EventArgs e)
    {
      Reload();
      Calculate();
    }

    void Reload() { }

    void Calculate() { }

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

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