C# 如何在隐藏按钮上执行单击事件
在我的C#表中,我有两个按钮C# 如何在隐藏按钮上执行单击事件,c#,C#,在我的C#表中,我有两个按钮 button1.Hide() private void button2_Click(object sender, EventArgs e) { button1.PerformClick(); } button1在表单加载时隐藏,我希望button1背后的逻辑在隐藏时也能执行 只需让外部的函数变成另一个函数,就可以调用函数,尽管您隐藏了按钮1 private void button1(object sender, EventArgs e) { _button1
button1.Hide()
private void button2_Click(object sender, EventArgs e)
{
button1.PerformClick();
}
button1在表单加载时隐藏,我希望button1背后的逻辑在隐藏时也能执行 只需让外部的函数变成另一个函数,就可以调用函数,尽管您隐藏了按钮1
private void button1(object sender, EventArgs e)
{
_button1();
}
private void button2(object sender, EventArgs e)
{
_button1();
}
//Here is the function
void _button1()
{
...
}
只需让外部的函数变成另一个函数,就可以调用函数,尽管您隐藏了按钮1
private void button1(object sender, EventArgs e)
{
_button1();
}
private void button2(object sender, EventArgs e)
{
_button1();
}
//Here is the function
void _button1()
{
...
}
如果你的按钮是隐藏的,你似乎需要背后的功能,而不仅仅是在特殊情况下。将功能排除在事件之外通常是避免将来出现问题的简单解决方案
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
// ....
}
即使您需要其他工作流的功能,此解决方案也可能更好
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() { }
如果你的按钮是隐藏的,你似乎需要背后的功能,而不仅仅是在特殊情况下。将功能排除在事件之外通常是避免将来出现问题的简单解决方案
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
// ....
}
即使您需要其他工作流的功能,此解决方案也可能更好
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() { }